iOS 富文本点击事件
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;
_textview.selectable = 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
posted on 2016-06-13 15:32 sunyaxue 阅读(19168) 评论(4) 编辑 收藏 举报