富文本及点击方法

类似这种效果及功能。

 

 

static NSString *const kStringTip = @”如有问题,请拨打客服电话:010-57512156″;


  1. _tip = [[UITextView alloc] init];
  2. _tip.width = SCREEN_WIDTH - kSideOffset * 2.0;
  3. _tip.text = kStringTip;
  4. [_tip sizeToFit];
  5. _tip.top = labelY + 29;
  6. _tip.left = kSideOffset;
  7. _tip.delegate = self;
  8. [self.view addSubview:_tip];
  9. NSString * creat = @"010-57512156";
  10. NSString * showLabel = [NSString stringWithFormat:@"%@",kStringTip];
  11. _tip.userInteractionEnabled = YES;
  12. NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:showLabel];
  13. NSRange range = [showLabel rangeOfString:creat];
  14. [AttributedStr addAttribute:NSForegroundColorAttributeName
  15. value:COLOR_GRAY3
  16. range:NSMakeRange(range.location, range.length)];//字体颜色
  17. [AttributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(range.location, range.length)];//字加下划线
  18. [AttributedStr addAttribute:NSLinkAttributeName value:@"tel://" range:NSMakeRange(range.location, range.length)];//加点击事件
  19. _tip.attributedText = AttributedStr;
  20. _tip.editable = NO;//这个一定要设置
  21. _tip.userInteractionEnabled = YES;
  22. _tip.textColor = COLOR_GRAY3;//在设置完富文本之后设置textview的字体颜色才会生效
  23. _tip.font = [UIFont systemFontOfSize:12];//在设置完富文本之后设置textview的字体大小才会生效

代理方法

  1. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
  2. if ([[URL scheme] isEqualToString:@"tel"]){
  3. NSString *phoneNumber = [NSString stringWithFormat:@"telprompt://%@",@"010-57512156"];
  4. NSURL *url = [NSURL URLWithString:phoneNumber];
  5. if ([[UIApplication sharedApplication]canOpenURL:url]) {
  6. [[UIApplication sharedApplication]openURL:url];
  7. }
  8. return NO;
  9. }
  10. return YES;
  11. }

 

版权声明:本文为miaomiaocat原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/miaomiaocat/p/6133243.html