NSTextField subclass issues

Discussion in 'Mac Programming' started by Monaj, Jan 2, 2012.

  1. macrumors regular

    May 24, 2009
    Hi all,

    I am using below code to subclass NSTextField-

    - (void)drawRect:(NSRect)dirtyRect
        // black outline
        NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
        NSGradient *gradient = nil;
        if ([NSApp isActive]) {
            gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.24 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.374 alpha:1.0]];
        else {
            gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.55 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.558 alpha:1.0]];
        [gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:7.6 yRadius:7.6] angle:90];
        // top inner shadow
        NSRect shadowFrame = NSMakeRect(1, 1, [self bounds].size.width-2.0, 10.0);
        [[NSColor colorWithCalibratedWhite:0.88 alpha:1.0] set];
        [[NSBezierPath bezierPathWithRoundedRect:shadowFrame xRadius:6.9 yRadius:6.9] fill];
        // draw the keyboard focus ring if we're the first responder and the application is active
        if (([[self window] firstResponder] == [self currentEditor]) && [NSApp isActive])
            [NSGraphicsContext saveGraphicsState];
            [[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:8.6 yRadius:8.6] fill]; 
            [NSGraphicsContext restoreGraphicsState];
        // main white area
        NSRect whiteFrame = NSMakeRect(1, 2, [self bounds].size.width-3.0, [self bounds].size.height-7.0);
        [[NSColor whiteColor] set];
        [[NSBezierPath bezierPathWithRoundedRect:whiteFrame xRadius:8.6 yRadius:8.6] fill];
        NSFont *stringFont = [NSFont fontWithName:@"Lucida Grande" size:18.0];
        if ([[self stringValue] length] == 0) {
            NSColor *txtColor = [NSColor grayColor];
            NSDictionary *placeHolderDict = [NSDictionary
                                             dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName,stringFont,NSFontAttributeName,
            placeHolderString = [[NSAttributedString alloc]
                                 initWithString:[[self cell] placeholderString] attributes:placeHolderDict];
            [placeHolderString drawAtPoint:NSMakePoint(10.0,0.0)];      
        else {
            NSColor *txtColor = [NSColor blackColor];
            NSDictionary *txtDict = [NSDictionary
                                     dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName,stringFont,NSFontAttributeName,
            placeHolderString = [[NSAttributedString alloc]
                                 initWithString:[self stringValue] attributes:txtDict];
            [placeHolderString drawAtPoint:NSMakePoint(6.0,4.0)];       
    But I am facing two problems:

    First Problem: When a text field is selected, it is showing overlapping white edges over round corners as shown in below image:


    Second Problem: When cursor is in textField1 and I am pressing tab key, it is not moving to textField2 though in IB I have binded next key view of textField1 as textField2.

    Can any one suggest me some solution for above problems?


Share This Page