1 Star 0 Fork 1

Develop_Man / TTTAttributedLabel

forked from 宿命 / TTTAttributedLabel 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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
  • Compatible with arm64 architecture when compiled with $(ARCHS_STANDARD_INCLUDING_64_BIT)
  • Automatic data detection
  • Manual link embedding
  • Label style inheritance for attributed strings

It also includes advanced paragraph style properties:

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

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

  • If you need help, use Stack Overflow. (Tag 'tttattributedlabel')
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

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.enabledTextCheckingTypes = 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, or higher. 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.

For arm64 compatibility, you must compile your project with the iOS 7 SDK.

Contact

Mattt Thompson

License

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

Copyright (c) 2011 Mattt Thompson (http://mattt.me/) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A drop-in replacement for UILabel that supports attributes, data detectors, links, and more fork from https://github.com/mattt/TTTAttributedLabel 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/Develop_Man/TTTAttributedLabel.git
git@gitee.com:Develop_Man/TTTAttributedLabel.git
Develop_Man
TTTAttributedLabel
TTTAttributedLabel
master

搜索帮助