TTTAttributedLabel Documentation

repository·master·Indexed 27 days ago

https://github.com/tttattributedlabel/tttattributedlabel

A drop-in replacement for UILabel providing advanced support for attributed strings, automatic data detection for links and phone numbers, and manual link embedding. Includes support for VoiceOver accessibility as of version 1.10.0 and configuration via Interface Builder using IBDesignable and IBInspectable.

Tokens
954
Snippets
3
Records
6
Agent score
44%

What's inside TTTAttributedLabel

  1. Configure TTTAttributedLabel in Interface Builder

    master

    The label supports IBDesignable and IBInspectable for configuration within Interface Builder.

    If you encounter errors like Failed to load designables from path (null), it is likely because you are using the library as a static library. To fix this, you should:

    • Install as a dynamic framework using CocoaPods with use_frameworks! in your Podfile.
    • Or use Carthage.
    • Or drag the source files directly into your project.
  2. Configure text with label attribute inheritance

    master

    To use a plain NSString while inheriting the label's existing properties (like font or textColor) and applying custom attributes to specific ranges, use the -setText:afterInheritingLabelAttributesAndConfiguringWithBlock: method. This allows you to modify a NSMutableAttributedString before it is rendered.

    ```objc
    // NSString
    TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor darkGrayColor];
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;
    
    // If you're using a simple `NSString` for your text, 
    // assign to the `text` property last so it can inherit other label properties.
    NSString *text = @"Lorem ipsum dolor sit amet";
    [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
      NSRange boldRange = [[mutableAttributedString string] rangeOfString:@
  3. Enable automatic link detection and manual link embedding

    master

    TTTAttributedLabel supports automatic data detection (URLs, phone numbers, etc.) and manual link embedding.

    1. Set enabledTextCheckingTypes to NSTextCheckingTypeLink for automatic detection.
    2. Set the delegate to receive tap events via the TTTAttributedLabelDelegate protocol.
    3. Use addLinkToURL:withRange: to manually embed a link in a specific substring.
  4. Set text in TTTAttributedLabel

    master

    You can display both plain and attributed text by passing an NSString or NSAttributedString to the setText: setter.

    Important: Never assign directly to the attributedText property; always use the text property or the specialized setText:afterInheritingLabelAttributesAndConfiguringWithBlock: method.

    ```objc
    // For NSAttributedString
    TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@
  5. Accessibility support for VoiceOver

    master

    As of version 1.10.0, TTTAttributedLabel supports VoiceOver via the UIAccessibilityElement protocol.

    • Each link is individually selectable.
    • The accessibilityLabel is set to the link's string value.
    • The accessibilityValue is set to the URL, phone number, or date value.

    To provide custom accessibility values, create a subclass and override accessibilityElements.