您的位置:首页 > 产品设计 > UI/UE

富文本(TTTAttributedLabel是一个替代,UILabel提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与NSTextCheckingTypes手动通过指定)

2017-02-23 10:04 2246 查看
转自:https://github.com/TTTAttributedLabel/TTTAttributedLabel

一个下拉更换为
UILabel
支持的属性,数据检测器,链接等

TTTAttributedLabel
是一个替代,
UILabel
提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与
NSTextCheckingTypes
手动通过指定URL,地址,电话号码,事件或公共交通信息的范围。

即使在iOS 6中
UILabel
收到支持
NSAttributedString
TTTAttributedLabel
有几个独特的功能:
自动数据检测
手动链接嵌入
标记属性字符串的样式继承
标签中链接的自定义样式
长按手势除了点击手势的链接

它还包括高级段落样式属性:
attributedTruncationToken

firstLineIndent

highlightedShadowRadius

highlightedShadowOffset

highlightedShadowColor

lineHeightMultiple

lineSpacing

minimumLineHeight

maximumLineHeight

shadowRadius

textInsets

verticalAlignment


要求

iOS 8+ / tvOS 9+
Xcode 7+


辅助功能

从版本1.10.0,
TTTAttributedLabel
通过
UIAccessibilityElement
协议支持VoiceOver 。每个链接可以单独选择,
accessibilityLabel
等于其字符串值,以及相应
accessibilityValue
的URL,电话号码和日期链接。希望更改此行为或提供自定义值的开发人员应创建子类并覆盖
accessibilityElements


通讯

如果需要帮助,请使用Stack
Overflow。(Tag 
tttattributedlabel

如果你想问一个一般的问题,使用Stack
Overflow。
如果您发现错误,请打开一个问题。
如果您有功能请求,请打开问题。
如果您想贡献,请提交拉取请求。


安装

CocoaPods是推荐的安装方法
TTTAttributedLabel
。只需将以下行添加到您的
Podfile


# Podfile

荚' TTTAttributedLabel “



用法

TTTAttributedLabel
可以显示纯文本和属性文本:只需传递一个
NSString
NSAttributedString
setText:
设置器。切勿分配到
attributedText
酒店。

// NSAttributedString

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

NSAttributedString * attString = [[ NSAttributedString  alloc ] initWithString:@“ Tom Bombadil ”
属性: @ {
(id)kCTForegroundColorAttributeName:(id)[UIColor redColor ]。CGColor,
NSFontAttributeName:[UIFont boldSystemFontOfSize:16 ],
NSKernAttributeName:[ NSNull  null ],
(id)kTTTBackgroundFillColorAttributeName:(id)[UIColor greenColor ]。CGColor
}];

//直接设置属性字符串,而不继承标签的任何其他文本
//属性。
attributLabel.text = attString;


// NSString

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

//如果你为你的文本使用一个简单的NSString,
//最后分配给`text`属性,所以它可以继承其他标签属性。
NSString * text = @“ Lorem ipsum dolor sit amet ” ;
[label setText: text afterInheritingLabelAttributesAndConfiguringWithBlock: ^ NSMutableAttributedString *(NSMutableAttributedString * mutableAttributedString){
NSRange boldRange = [[mutableAttributedString string ] rangeOfString:@“ ipsum dolor ”  options:NSCaseInsensitiveSearch ];
NSRange strikeRange = [[mutableAttributedString string ] rangeOfString:@“ sit amet ”  options:NSCaseInsensitiveSearch ];

// Core Text API使用C函数,而不需要直接连接到UIFont。请参阅Apple的“核心文本编程指南”,了解如何配置字符串属性。
UIFont * boldSystemFont = [UIFont boldSystemFontOfSize:14 ];
CTFontRef字体= CTFontCreateWithName((__bridge CFStringRef)boldSystemFont。的fontName,boldSystemFont。的pointsize,NULL);
if(font){
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName  value:(__bridge id)font range: boldRange];
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName  value: @ YES  range: strikeRange];
CFrelease(font);
}}

return mutableAttributedString;
}];


首先,我们创建和配置标签,与实例化的方式相同
UILabel
。在使用
-setText:afterInheritingLabelAttributesAndConfiguringWithBlock:
方法时,在标签上设置的任何文本属性都将继承为基本属性。在该示例中,子串“ipsum
dolar”将以粗体显示,使得标签将读作“Lorem ipsum dolar sit amet”,大小为14Helvetica,具有深灰色。


IBDesignable

TTTAttributedLabel
包含
IBInspectable
IB_DESIGNABLE
注释以在Interface
Builder中配置标签。但是,如果您在建立时看到这些警告...
IB Designables: Failed to update auto layout status: Failed to load designables from path (null)
IB Designables: Failed to render instance of TTTAttributedLabel: Failed to load designables from path (null)


...那么你可能使用
TTTAttributedLabel
作为静态库,它不支持IB注释。一些解决方法包括:
安装
TTTAttributedLabel
与使用的CocoaPods一个动态的框架
use_frameworks!
在你的
Podfile
,或者与迦太基
安装
TTTAttributedLabel
它的源文件拖放到你的项目


链接和数据检测

除了支持富文本,
TTTAttributedLabel
还可以自动检测日期,地址,网址,电话号码,公交信息的链接,并允许您嵌入自己的链接。

label.enabledTextCheckingTypes = NSTextCheckingTypeLink ; //当标签文本随后更改时自动检测链接
label.delegate = self; //当用户点击链接时调用委托方法(参见`TTTAttributedLabelDelegate`协议)

label.text = @“ Fork me on GitHub!(https://github.com/mattt/TTTAttedLabel/)” ; //存储库URL将被自动检测和链接

NSRange range = [label.text rangeOfString:@“ me ” ];
[label addLinkToURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://hotgood-detail?id=%@",model.roomId] withRange: range]; //在子字符串中嵌入自定义链接


执行代理方法

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL
*)url{

if ([url.schemeisEqualToString:@"http"]) {
        NSNumber *vId = (NSNumber*)[url queryParam:@"id"];
        
        if([url.hostisEqualToString:@"hotgood-detail"]){
            //跳转页面
           
        }else {
           //跳转页面
            
        }
}


演示

pod test TTTAttributedLabel


...或克隆此repo并构建和运行/测试
Espressos
项目在Xcode看到
TTTAttributedLabel
在行动。如果你没有的CocoaPods安装,以抓住它
[sudo]
gem install cocoapods


cd示例
pod安装
打开Espressos.xcworkspace



执照

TTTAttributedLabel
根据MIT许可证提供。有关详细信息,请参阅LICENSE文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐