TextView 实现富文本 可以点击
2017-12-15 08:37
459 查看
转自:http://www.cnblogs.com/sunyaxue/p/5580807.html
1 #import "ViewController.h" 2 3 #define font 17 4 @interface ViewController ()<UITextViewDelegate> 5 @property (weak, nonatomic) IBOutlet UITextView *textview; 6 @property (assign, nonatomic) BOOL isSelect; 7 @end 8 9 @implementation ViewController 10 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 [self protocolIsSelect:self.isSelect]; 15 16 } 17 18 - (void)protocolIsSelect:(BOOL)select { 19 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"请遵守以下协议《支付宝协议》《微信协议》《建行协议》《招行协议》《中国银行协议》《上海银行协议》"]; 20 [attributedString addAttribute:NSLinkAttributeName 21 value:@"zhifubao://" 22 range:[[attributedString string] rangeOfString:@"《支付宝协议》"]]; 23 [attributedString addAttribute:NSLinkAttributeName 24 value:@"weixin://" 25 range:[[attributedString string] rangeOfString:@"《微信协议》"]]; 26 [attributedString addAttribute:NSLinkAttributeName 27 value:@"jianhang://" 28 range:[[attributedString string] rangeOfString:@"《建行协议》"]]; 29 30 31 UIImage *image = [UIImage imageNamed:select == YES ? @"new_feature_share_true" : @"new_feature_share_false"]; 32 CGSize size = CGSizeMake(font + 2, font + 2); 33 UIGraphicsBeginImageContextWithOptions(size, false, 0); 34 [image drawInRect:CGRectMake(0, 2, size.width, size.height)]; 35 UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext(); 36 UIGraphicsEndImageContext(); 37 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 38 textAttachment.image = resizeImage; 39 NSMutableAttributedString *imageString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment]; 40 [imageString addAttribute:NSLinkAttributeName 41 value:@"checkbox://" 42 range:NSMakeRange(0, imageString.length)]; 43 [attributedString insertAttributedString:imageString atIndex:0]; 44 [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, attributedString.length)]; 45 _textview.attributedText = attributedString; 46 _textview.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], 47 NSUnderlineColorAttributeName: [UIColor lightGrayColor], 48 NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; 49 50 _textview.delegate = self; 51 _textview.editable = NO; //必须禁止输入,否则点击将弹出输入键盘 52 _textview.scrollEnabled = NO; 53 } 54 55 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 56 if ([[URL scheme] isEqualToString:@"jianhang"]) { 57 NSLog(@"建行支付---------------"); 58 return NO; 59 } else if ([[URL scheme] isEqualToString:@"zhifubao"]) { 60 NSLog(@"支付宝支付---------------"); 61 return NO; 62 } else if ([[URL scheme] isEqualToString:@"weixin"]) { 63 NSLog(@"微信支付---------------"); 64 return NO; 65 } else if ([[URL scheme] isEqualToString:@"checkbox"]) { 66 self.isSelect = !self.isSelect; 67 [self protocolIsSelect:self.isSelect]; 68 return NO; 69 } 70 return YES; 71 } 72 73 74 @end
相关文章推荐
- TextView 部分文字高亮、点击、添加超链接、替换字体、富文本、图片替换文字等实现
- 可以响应各个方向CompoundDrawables点击操作的TextView的实现原理
- textview第一次出现不可滚动文本,但是点击出现键盘,键盘落下,就可以滚动问题
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
- Android - TextView 实现部分文本点击事件
- 可以响应各个方向CompoundDrawables点击操作的TextView的实现原理
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
- Android中实现为TextView添加多个可点击的文本
- Android TextView中实现点击文本超链接(无下划线)的封装类
- Android实战,实现文本读取,实现assets下资源文本的读取,实现l列表下隐藏TextView点击显示
- 一段文本内容中展示网址链接---TTTAttributedLabel链接点击和textView两种方式实现
- android中TextView实现多个可点击的文本
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
- 用TextView实现富文本展示,点击断句和语音播报
- TextView添加链接实现富文本label带点击功能
- Android中实现为TextView添加多个可点击的文本
- 解决RecyclerView实现聊天界面,但点击下面的EditText后弹出的输入法会遮盖RecyclerView内容的方法
- Android TextView富文本、超链接和添加文字背景等其他效果的实现
- (转)监听TextView中链接文本的点击事件