首页 > 其他 > 详细

富文本及点击方法

时间:2016-12-05 11:42:37      阅读:277      评论:0      收藏:0      [点我收藏+]

富文本及点击方法

技术分享

类似这种效果及功能。

 

 

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

 

富文本及点击方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!