Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Created June 23, 2012 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jverkoey/2977370 to your computer and use it in GitHub Desktop.
Save jverkoey/2977370 to your computer and use it in GitHub Desktop.
NIAttributedLabel Example 1
// We create an NIAttributedLabel the same way we would a UILabel.
NIAttributedLabel* label = [[NIAttributedLabel alloc] initWithFrame:CGRectZero];
// In practice we set the text before applying any CoreText style. Modifying the text after
// setting the styles will clear any existing CoreText-specific styles.
//
// Peeking under the hood: NIAttributedLabel creates an NSMutableAttributedString object when we
// set this text. The NSMutableAttributedString object is initially styled with whatever values
// were set on the UILabel. For example, if we set the textColor to blue and then set the text to
// @"Nimbus", the label would correctly display the text as blue. This allows you to treat
// NIAttributedLabel as a UILabel but still easily be able to style its attributed string with
// additional CoreText properties.
label.text = @"An explorer's tale";
// UIViewAutoresizingFlexibleDimensions is a Nimbus autoresizing mask that causes the view to
// grow and shrink with its super view. When we want to a view to fill its super view this is
// generally the mask that we'll use.
label.autoresizingMask = UIViewAutoresizingFlexibleDimensions;
// These are standard UILabel styles. We can set these whenever we like and the attributed label
// will apply them to entire string even if we change the text again.
label.font = [UIFont fontWithName:@"Zapfino" size:25];
// Set the text color to the bright Nimbus orange. This will be the "fill" color when we specify
// a negative stroke width below.
label.textColor = RGBCOLOR(0xFF, 0x9F, 0x00);
// Set a CoreText stroke color to a dark orange with 40% opacity.
label.strokeColor = RGBACOLOR(0x93, 0x36, 0x00, 0.4);
// A negative stroke width means that we also want to fill the stroke with the textColor.
// A postiive stroke would leave the fill as the background color.
label.strokeWidth = -3;
// Fill the view with the label, but give the label a 20 pixel margins on all sides.
label.frame = CGRectInset(self.view.bounds, 20, 20);
[self.view addSubview:label];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment