Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Created February 26, 2012 12:15
Show Gist options
  • Save TomLiu/1916376 to your computer and use it in GitHub Desktop.
Save TomLiu/1916376 to your computer and use it in GitHub Desktop.
Cocoa label with underline
NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(0, 0, 320, 25)];
[label setEditable:NO];
[label setBordered:NO];
[label setFont:[NSFont systemFontOfSize:17.0]];
[label setBackgroundColor:[NSColor clearColor]];
[label setTextColor:[NSColor blackColor];
[label.cell setLineBreakMode:NSLineBreakByTruncatingTail];
[label setAutoresizingMask:NSViewMaxXMargin|NSViewMaxYMargin|NSViewWidthSizable];
NSFont *font = [NSFont systemFontOfSize:14.0];
NSNumber *underLineStyle = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:underLineStyle,font,[NSColor redColor], nil]
forKeys:[NSArray arrayWithObjects:NSUnderlineStyleAttributeName,NSFontAttributeName,NSForegroundColorAttributeName, nil]];
NSAttributedString *attrsString = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Details and Edit", @"") attributes:attrsDictionary];
[label setAttributedStringValue:attrsString];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment