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

31 篇文章 0 订阅
7 篇文章 0 订阅

转自:https://github.com/TTTAttributedLabel/TTTAttributedLabel


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

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

即使在iOS 6中UILabel收到支持NSAttributedStringTTTAttributedLabel有几个独特的功能:

  • 自动数据检测
  • 手动链接嵌入
  • 标记属性字符串的样式继承
  • 标签中链接的自定义样式
  • 长按手势除了点击手势的链接

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

  • 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可以显示纯文本和属性文本:只需传递一个NSStringNSAttributedStringsetText:设置器。切勿分配到attributedText酒店。

// NSAttributedString

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

NSAttributedString * attString = [[ NSAttributedString  alloc ] initWithString:@“ Tom Bombadil 
                                                                属性: @ {idkCTForegroundColorAttributeName:(id)[UIColor redColor ]。CGColorNSFontAttributeName:[UIFont boldSystemFontOfSize:16 ],
         NSKernAttributeName:[ NSNull  null ],idkTTTBackgroundFillColorAttributeName:(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。的pointsizeNULL);
  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包含IBInspectableIB_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文件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值