iOS 富文本点击事件

来源:互联网 发布:阿里巴巴网络银行 编辑:程序博客网 时间:2024/05/16 13:56
复制代码
 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 ViewController10 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:NSLinkAttributeName21                              value:@"zhifubao://"22                              range:[[attributedString string] rangeOfString:@"《支付宝协议》"]];23     [attributedString addAttribute:NSLinkAttributeName24                              value:@"weixin://"25                              range:[[attributedString string] rangeOfString:@"《微信协议》"]];26     [attributedString addAttribute:NSLinkAttributeName27                              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:NSLinkAttributeName41                              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
复制代码

0 0