富文本及点击方法

Posted 喵喵yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了富文本及点击方法相关的知识,希望对你有一定的参考价值。

富文本及点击方法

类似这种效果及功能。

 

 

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

 

以上是关于富文本及点击方法的主要内容,如果未能解决你的问题,请参考以下文章

Android中富文本用法包括点击事件处理

Android中富文本用法包括点击事件处理

vue 实现点击复制文本

富文本编辑器宽度自适应及取消自动保存功能

matplotlib 可视化必知必会富文本绘制方法

matplotlib可视化必知必会富文本绘制方法