美文网首页
UILable如何实现加载带链接的富文本,同时可以跳转

UILable如何实现加载带链接的富文本,同时可以跳转

作者: 爱的就是娜 | 来源:发表于2023-12-20 17:28 被阅读0次

这边的需求呢,就是文本中会出现带人名的特殊显示,而且点击人名能够拨打电话

我这弄了半天,发现使用lable弄了半天都没法实现这个,然后,我就用textView实现了,效果与lable没区别

// 初始化

self.noteTextView = [[UITextView alloc] init];

 self.noteTextView.backgroundColor = [UIColor clearColor];

            self.noteTextView.editable=NO;//设置为不可编辑

            self.noteTextView.scrollEnabled=NO;

            self.noteTextView.delegate=self;//设置代理(<UITextViewDelegate>)

            self.noteTextView.textContainerInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f);//控制距离上下左右的边距

NSString * nameStr = @"张三";

 NSString * ownStr = [NSString stringWithFormat:@"<a href='tel://%@' style='color: #3D8777; text-decoration: none'>%@</a>",@"13311209976",@"张三"];

    NSMutableString * mutableStr = [NSMutableString stringWithString:@"张三想屁吃"];

[mutableStr replaceOccurrencesOfString:nameStr withString:ownStr options:NSLiteralSearch range:NSMakeRange(0, mutableStr.length)];

 //HTML格式的文本

    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithData:[mutableStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];

    NSMutableParagraphStyle *style = [attributedString attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];

    style.alignment = NSTextAlignmentLeft;

    self.noteTextView.attributedText= attributedString;

// 设置人名的颜色,默认为.a标签的蓝色

    self.noteTextView.linkTextAttributes = @{NSForegroundColorAttributeName:MainStyleColor};

// 代理方法

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRange {

    //URL:就是那个跳转链接

    NSString* abslutStr = URL.absoluteString;

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:abslutStr]];

//    NSString * phoneStr = [URL.absoluteString stringByReplacingOccurrencesOfString:@"tel://" withString:@""];

//    if(phoneStr.length) {

  //      [NRContactTool callNumber:phoneStr withPrompt:NO];

//    }

    //返回NO,如果返回YES就会跳到Safari

    return NO;

}

相关文章

  • UILable 加载富文本并计算高度-Swift

    利用UILable 加载富文本并计算高度-Swift

  • iOS 富文本加载和上传

    需要在UIlable 和UItextView 上加载带有HTML的富文本 NSData *data = [str...

  • 图文混排三种境界

    目标:显示富文本,包括文字、图片、链接等,图片需要点击预览,链接可以正常跳转。 境界一. 采用UILabel实现 ...

  • 不知道的知识点。

    1.如何对NGUI的文本加下划线? UILable控件可以添加富文本格式,原因在于他有BBcode富文本编辑器 我...

  • egret知(填)识(坑)点集

    1. egret.Label 富文本用法 富文本还可以添加链接,同时可以给链接添加下划线哦 ^0^ 给链接添加事件...

  • WebView shouldOverrideUrlLoading

    需求 在前端富文本编译器中,插入url,在Android端 点击链接跳转到新页面,并加载url。 分析 要是能拿到...

  • iOS富文本段落、链接点击跳转url

    初始化(MyClass) 富文本 段落 链接 富文本赋值给TextView 实现点击链接后的操作

  • UILable富文本

    富文本 NSString*str =@"人生若只如初见,何事秋风悲画扇。\n等闲变却故人心,却道故人心易变。\n骊...

  • Apple Documentation <Foundati

    富文本NSAttributedString,有属性的字符串,可以设置字体,字号,颜色等属性。 以UILable为例...

  • UILable的富文本

    最近碰到了一个关于用一个label显示换行与文字多中颜色显示的问题,本人第一想到的就是富文本NSAttribute...

网友评论

      本文标题:UILable如何实现加载带链接的富文本,同时可以跳转

      本文链接:https://www.haomeiwen.com/subject/actxndtx.html