|
Nimbus
master - Fork Nimbus on Github - Visit the Nimbus Wiki
The iOS framework that grows only as fast as its documentation
|
Classes | |
| class | NIAttributedLabel |
| The NIAttributedLabel class provides support for displaying rich text with selectable links. More... | |
| protocol | <NIAttributedLabelDelegate> |
| The methods declared by the NIAttributedLabelDelegate protocol allow the adopting delegate to respond to messages from the NIAttributedLabel class and thus respond to selections. More... | |
The Nimbus Attributed Label is a UILabel that uses NSAttributedString to render rich text labels with links using CoreText.
Required frameworks:
Minimum Operating System: iOS 4.0
Source located in src/attributedlabel/src
#import "NimbusAttributedLabel.h"
NIAttributedLabel is a subclass of UILabel. The attributed label maintains an NSAttributedString object internally which is used in conjunction with CoreText to draw rich-text labels. A number of helper methods for modifying the text style are provided. If you need to directly modify the internal NSAttributedString you may do so by accessing the attributedString property on iOS 4 through 5.*.
attributedText property now provided via UILabel.NIAttributedLabel* label = [[NIAttributedLabel alloc] initWithFrame:CGRectZero]; // The internal NSAttributedString will inherit all of UILabel's text attributes when we assign // text. label.text = @"Nimbus";
You can use an attributed label within Interface Builder by creating a UILabel and changing its class to NIAttributedLabel. This will allow you to set styles that apply to the entire string. If you want to style specific parts of the string then you will need to do this in code.
Automatic link detection is provided using NSDataDetector. Link detection is off by default and can be enabled by setting autoDetectLinks to YES. You may configure the types of data that are detected by modifying the dataDetectorTypes property. By default only urls will be detected.
// Enable link detection on the label.
myLabel.autoDetectLinks = YES;
Enabling automatic link detection will automatically enable user interation with the label view so that the user can tap the detected links.
Detected links will use linkColor and highlightedLinkColor to differentiate themselves from standard text. highlightedLinkColor is the color of the highlighting frame around the text. You can easily add underlines to links by enabling linksHaveUnderlines. You can customize link attributes in more detail by directly modifying the attributesForLinks property.
Automatic link detection is expensive. You can choose to defer automatic link detection by enabling deferLinkDetection. This will move the link detection to a separate background thread. Once the links have been detected the label will be redrawn.
The NIAttributedLabelDelegate protocol allows you to handle when the user taps on a given link. The protocol methods provide the tap point as well as the data pertaining to the tapped link.
- (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point { [[UIApplication sharedApplication] openURL:result.URL]; }
Links can be added explicitly using addLink:range:.
// Add a link to the string 'nimbus' in myLabel. [myLabel addLink:[NSURL URLWithString:@"nimbus://custom/url"] range:[myLabel.text rangeOfString:@"nimbus"]];
To underline an entire label:
// Underline the whole label with a single line.
myLabel.underlineStyle = kCTUnderlineStyleSingle;
Underline modifiers can also be added:
// Underline the whole label with a dash dot single line.
myLabel.underlineStyle = kCTUnderlineStyleSingle;
myLabel.underlineStyleModifier = kCTUnderlinePatternDashDot;
Underline styles and modifiers can be mixed to create the desired effect, which is shown in the following screenshot:
NIAttributedLabel supports justified text using UITextAlignmentJustify.
myLabel.textAlignment = UITextAlignmentJustify;
myLabel.strokeWidth = 3.0; myLabel.strokeColor = [UIColor blackColor];
A positive stroke width will render only the stroke.
A negative number will fill the stroke with textColor:
myLabel.strokeWidth = -3.0; myLabel.strokeColor = [UIColor blackColor];
Kerning is the space between characters in points. A positive kern will increase the space between letters. Correspondingly a negative number will decrease the space.
myLabel.textKern = -6.0;
All styles that can be added to the whole label (as well as default UILabel styles like font and text color) can be added to just a range of text.
[myLabel setTextColor:[UIColor orangeColor] range:[myLabel.text rangeOfString:@"Nimbus"]]; [myLabel setFont:[UIFont boldSystemFontOfSize:22] range:[myLabel.text rangeOfString:@"iOS"]];