Coder Social home page Coder Social logo

maybe / tttattributedlabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tttattributedlabel/tttattributedlabel

0.0 2.0 0.0 945 KB

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

License: MIT License

tttattributedlabel's Introduction

TTTAttributedLabel

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

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

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

  • Compatibility with iOS >= 4.3
  • Automatic data detection
  • Manual link embedding
  • Label style inheritance for attributed strings

It also includes advanced paragraph style properties:

  • verticalAlignment
  • textInsets
  • firstLineIndent
  • leading
  • lineHeightMultiple
  • shadowRadius
  • highlightedShadowRadius / highlightedShadowOffset / highlightedShadowColor
  • truncationTokenString

Installation

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

Podfile

pod 'TTTAttributedLabel'

Usage

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((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
  if (font) {
    [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
    [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES] range:strikeRange];
    CFRelease(font);
  }

  return 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.

The normal setText: setter accepts both NSString and NSAttributedString; in the latter case, the attributed string is directly set, without inheriting the base style of the label.

Links and Data Detection

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

label.dataDetectorTypes = NSTextCheckingTypeLink; // 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]; // Embedding a custom link in a substring

Demo

Build and run the TTTAttributedLabelExample project in Xcode to see TTTAttributedLabel in action.

Requirements

TTTAttributedLabel is compatible with iOS 4.3+ as a deployment target, but must be compiled using the iOS 6 SDK. If you get compiler errors for undefined constants, try upgrading to the latest version of Xcode, and updating your project to the recommended build settings.

TTTAttributedLabel also requires the CoreText and Core Graphics frameworks. If you're installing with CocoaPods these frameworks will automatically be linked for you, otherwise you will have to add them to your project.

Contact

Mattt Thompson

License

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

tttattributedlabel's People

Contributors

adamjernst avatar ata-n avatar corillian avatar danimal avatar dennisreimann avatar evil159 avatar inamiy avatar jamztang avatar jasongregori avatar johnezang avatar leftspin avatar makdad avatar mattt avatar mojodna avatar nikolay-tymchenko avatar omnivector avatar pashields avatar petec avatar pitiphong-p avatar robbiehanson avatar runmad avatar scalessec avatar soffes avatar spenrose avatar tbonnin avatar tsafrir avatar tyrone-sudeium avatar vilemkurz avatar yimingtang avatar zdavison avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.