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

UILabel设置属性字符串,可以增加点击事件点击

2017-12-29 11:46 405 查看
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;
}

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 完美解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐