NSTextField subclass not working properly

  • 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];

            NSSetFocusRingStyle(NSFocusRingOnly);

            [[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,

                                            nil];

            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,

                                    nil];

            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:

    http://i.stack.imgur.com/8NJHW.png

    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?

    Thanks
  • On Jan 2, 2012, at 10:35 PM, Devarshi Kulshreshtha wrote:

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

    That's the field editor drawing its background. Tweak it in setUpFieldEditorAttributes:

    --
    Seth Willits
previous month january 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today