Tag: TTTAttributedLabel
git clone https://github.com/mattt/TTTAttributedLabel.git
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero]; label.font = [UIFont systemFontOfSize:14]; label.textColor = [UIColor darkGrayColor]; label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; NSString *text = @"Lorem ipsum dolar sit amet"; [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolar" options:NSCaseInsensitiveSearch]; NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch]; // Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes. UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14]; CTFontRef font = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); if (font) { [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange]; [mutableAttributedString addAttribute:@"TTTStrikeOutAttribute" value:[NSNumber numberWithBool:YES] range:strikeRange]; CFRelease(font); } return mutableAttributedString; }];
label.dataDetectorTypes = UIDataDetectorTypeAll; // Automatically detect links when the label text is subsequently changed label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol) label.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked NSRange range = [label.text rangeOfString:@"me"]; [label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedd
TTTAttributedLabel *tttLabel = <# create the label here #>; NSString *labelText = @"Lost? Learn more."; tttLabel.text = labelText; NSRange r = [labelText rangeOfString:@"Learn more"]; [tttLabel addLinkToURL:[NSURL URLWithString:@"action:show-help"] withRange:r]; - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { if ([[url scheme] hasPrefix:@"action"]) { if ([[url host] hasPrefix:@"show-help"]) { /* load help screen */ } else if ([[url host] hasPrefix:@"show-settings"]) { /* load settings screen */ } } else { /* deal with http links here */ } }