XAttributedString
示例
要运行示例项目,请先克隆仓库,然后在示例目录中运行 pod install
需求
安装
XAttributedString 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中:
pod 'XAttributedString'
使用
// 设置属性字符
NSString *text = @"测试属性字符";
NSMutableAttributedString *mAtrStr = [[NSMutableAttributedString alloc] initWithString:text];
[mAtrStr x_addAttributeTextColor:[UIColor redColor] range:NSMakeRange(2, 2)];
[mAtrStr x_addAttributeFont:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 2)];
[mAtrStr x_addAttributeUnderlineStyle:NSUnderlineStyleSingle color:[UIColor blackColor] range:NSMakeRange(4, 2)];
[self.atrLab setAttributedText:mAtrStr];
// 设置属性字符
NSString *text2 = @"测试可点击字符";
NSMutableAttributedString *mAtrStr2 = [[NSMutableAttributedString alloc] initWithString:text2];
// 若点击位置不准确,可能通过以下方案解决,详情查看接口说明
[self.atrTapLab x_setDefaultFont:self.atrTapLab.font];
[mAtrStr2 x_addAttributeTextColor:[UIColor redColor] range:NSMakeRange(2, 3)];
[mAtrStr2 x_addAttributeFont:[UIFont systemFontOfSize:40] range:NSMakeRange(0, 1)];
[self.atrTapLab setAttributedText:mAtrStr2];
// 设置可点击区域
[self.atrTapLab x_addTapEvent:^(NSUInteger index, NSAttributedString * _Nullable attrStr) {
NSLog(@"====== index: %ld, str: %@", index, attrStr);
} range:NSMakeRange(2, 3)];
作者
papfish, [email protected]
许可
XAttributedString在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。