1.首先啊,label可以设置属性字符串,设置不同的字体颜色,字体大小,但是要增加点击事件,不好操作。
2.所以文章是设置textView来完成的。
- (UITextView *)textView {
if (!_textView) {
_textView = [[UITextView alloc] init];
// NSMutableAttributedString *attributedString = [NSString attributuStringWithString:self.descText lineSpacing:10];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1.您可能很久未更新最新数据导致图表有误,请及时刷新;\n以上分析根据公积金官网数据统计分析所得,可能存在有误。"];
[attributedString addAttribute:NSLinkAttributeName
value:@"ProvidentFundshuaxin://"
range:[[attributedString string] rangeOfString:@"刷新"]];
[attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} range:[self.descText rangeOfString:@"刷新"]];
_textView.attributedText = attributedString;
_textView.linkTextAttributes = @{NSForegroundColorAttributeName: ColorFromHexRGB(0xe73c00),
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
_textView.textColor = ColorFromHexRGB(0xa0a0a0);
_textView.delegate = self;
_textView.editable = NO; //必须禁止输入,否则点击将弹出输入键盘
_textView.scrollEnabled = NO;
// NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
// paragraphStyle.lineSpacing = 20;// 字体的行间距
// NSDictionary *attributes = @{
// NSFontAttributeName:[UIFont systemFontOfSize:12],
// NSParagraphStyleAttributeName:paragraphStyle
// };
// _textView.typingAttributes = attributes;
// _textView.layer.borderColor = [UIColor cyanColor].CGColor;
// _textView.layer.borderWidth = 1.0f;
}
return _textView;
}
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
NSRange range = [@"1.您可能很久未更新最新数据导致图表有误,请及时刷新;\n以上分析根据公积金官网数据统计分析所得,可能存在有误。" rangeOfString:@"刷新"];
if (NSEqualRanges(characterRange, range)) {
NSLog(@"设置您的自定义事件");
// if (self.refreshAccountBlock) {
// self.refreshAccountBlock();
// }
}
return YES;
}ok 完美解决。
该博客介绍如何利用TextView替代UILabel,设置属性字符串并添加点击事件。通过这种方法,可以方便地为不同部分的文本设置不同的样式,并实现点击交互。
1702

被折叠的 条评论
为什么被折叠?



