码迷,mamicode.com
首页 > 其他好文 > 详细

富文本及点击方法

时间:2016-12-05 11:44:20      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:enum   elf   alt   phone   offset   方法   文本   shared   ble   

富文本及点击方法

技术分享

类似这种效果及功能。

 

 

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;
}

 

富文本及点击方法

标签:enum   elf   alt   phone   offset   方法   文本   shared   ble   

原文地址:http://www.cnblogs.com/miaomiaocat/p/6133243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!