wireapp/TTTAttributedLabel

Name: TTTAttributedLabel

Owner: Wire Swiss GmbH

Description: A drop-in replacement for UILabel that supports attributes, data detectors, links, and more

Forked from: wearezeta/TTTAttributedLabel

Created: 2016-07-14 14:37:15.0

Updated: 2017-10-07 16:32:13.0

Pushed: 2017-01-10 14:57:50.0

Homepage:

Size: 1636

Language: Objective-C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

TTTAttributedLabel

Circle CI Version Status codecov license MIT Platform Carthage compatible

A drop-in replacement for UILabel that supports attributes, data detectors, links, and more

TTTAttributedLabel is a drop-in replacement for UILabel providing a simple way to performantly render attributed strings. As a bonus, it also supports link embedding, both automatically with NSTextCheckingTypes and manually by specifying a range for a URL, address, phone number, event, or transit information.

Even though UILabel received support for NSAttributedString in iOS 6, TTTAttributedLabel has several unique features:

It also includes advanced paragraph style properties:

Requirements
Accessibility

As of version 1.10.0, TTTAttributedLabel supports VoiceOver through the UIAccessibilityElement protocol. Each link can be individually selected, with an accessibilityLabel equal to its string value, and a corresponding accessibilityValue for URL, phone number, and date links. Developers who wish to change this behavior or provide custom values should create a subclass and override accessibilityElements.

Communication
Installation

CocoaPods is the recommended method of installing TTTAttributedLabel. Simply add the following line to your Podfile:

dfile

'TTTAttributedLabel'
Usage

TTTAttributedLabel can display both plain and attributed text: just pass an NSString or NSAttributedString to the setText: setter. Never assign to the attributedText property.

SAttributedString

ttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];

tributedString *attString = [[NSAttributedString alloc] initWithString:@"Tom Bombadil"
                                                            attributes:@{
    (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
    NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
    NSKernAttributeName : [NSNull null],
    (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor


he attributed string is directly set, without inheriting any other text
roperties of the label.
ibutedLabel.text = attString;
bjc
SString

ttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
l.font = [UIFont systemFontOfSize:14];
l.textColor = [UIColor darkGrayColor];
l.lineBreakMode = NSLineBreakByWordWrapping;
l.numberOfLines = 0;

f you're using a simple `NSString` for your text,
ssign to the `text` property last so it can inherit other label properties.
ring *text = @"Lorem ipsum dolor sit amet";
el setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
Range boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolor" options:NSCaseInsensitiveSearch];
Range 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.
Font *boldSystemFont = [UIFont boldSystemFontOfSize:14];
FontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
 (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:@YES range:strikeRange];
CFRelease(font);


turn mutableAttributedString;

First, we create and configure the label, the same way you would instantiate UILabel. Any text properties that are set on the label are inherited as the base attributes when using the -setText:afterInheritingLabelAttributesAndConfiguringWithBlock: method. In this example, the substring “ipsum dolar”, would appear in bold, such that the label would read “Lorem ipsum dolar sit amet”, in size 14 Helvetica, with a dark gray color.

IBDesignable

TTTAttributedLabel includes IBInspectable and IB_DESIGNABLE annotations to enable configuring the label inside Interface Builder. However, if you see these warnings when building…

esignables: Failed to update auto layout status: Failed to load designables from path (null)
esignables: Failed to render instance of TTTAttributedLabel: Failed to load designables from path (null)

…then you are likely using TTTAttributedLabel as a static library, which does not support IB annotations. Some workarounds include:

Links and Data Detection

In addition to supporting rich text, TTTAttributedLabel can automatically detect links for dates, addresses, URLs, phone numbers, transit information, and allows you to embed your own links.

l.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
l.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)

l.text = @"Fork me on GitHub! (https://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked

nge range = [label.text rangeOfString:@"me"];
el addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
Demo
try TTTAttributedLabel

…or clone this repo and build and run/test the Espressos project in Xcode to see TTTAttributedLabel in action. If you don't have CocoaPods installed, grab it with [sudo] gem install cocoapods.

xample
install
 Espressos.xcworkspace
License

TTTAttributedLabel is available under the MIT license. See the LICENSE file for more info.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.