富文本及点击方法
富文本及点击方法
类似这种效果及功能。
static NSString *const kStringTip = @”如有问题,请拨打客服电话:010-57512156″;
_tip = [[UITextView alloc] init];- _tip.width = SCREEN_WIDTH - kSideOffset * 2.0;
- _tip.text = kStringTip;
- [_tip sizeToFit];
- _tip.top = labelY + 29;
- _tip.left = kSideOffset;
- _tip.delegate = self;
- [self.view addSubview:_tip];
- NSString * creat = @"010-57512156";
- NSString * showLabel = [NSString stringWithFormat:@"%@",kStringTip];
- _tip.userInteractionEnabled = YES;
- NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:showLabel];
- NSRange range = [showLabel rangeOfString:creat];
- [AttributedStr addAttribute:NSForegroundColorAttributeName
- value:COLOR_GRAY3
- range:NSMakeRange(range.location, range.length)];//字体颜色
- [AttributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(range.location, range.length)];//字加下划线
- [AttributedStr addAttribute:NSLinkAttributeName value:@"tel://" range:NSMakeRange(range.location, range.length)];//加点击事件
- _tip.attributedText = AttributedStr;
- _tip.editable = NO;//这个一定要设置
- _tip.userInteractionEnabled = YES;
- _tip.textColor = COLOR_GRAY3;//在设置完富文本之后设置textview的字体颜色才会生效
- _tip.font = [UIFont systemFontOfSize:12];//在设置完富文本之后设置textview的字体大小才会生效
代理方法
- - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
- if ([[URL scheme] isEqualToString:@"tel"]){
- NSString *phoneNumber = [NSString stringWithFormat:@"telprompt://%@",@"010-57512156"];
- NSURL *url = [NSURL URLWithString:phoneNumber];
- if ([[UIApplication sharedApplication]canOpenURL:url]) {
- [[UIApplication sharedApplication]openURL:url];
- }
- return NO;
- }
- return YES;
- }
版权声明:本文为miaomiaocat原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。