美文网首页UI进价iOS iOS
iOS 富文本添加点击事件

iOS 富文本添加点击事件

作者: anny_4243 | 来源:发表于2016-11-25 15:02 被阅读5770次

    http://www.cnblogs.com/sunyaxue/p/5580807.html

    #import "ViewController.h"
    
    #define font 17
    @interface ViewController ()<UITextViewDelegate>
    @property (weak, nonatomic) IBOutlet UITextView *textview;
    @property (assign, nonatomic) BOOL isSelect;
    @end
    
    @implementation ViewController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self protocolIsSelect:self.isSelect];
    
    }
    
    - (void)protocolIsSelect:(BOOL)select {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"请遵守以下协议《支付宝协议》《微信协议》《建行协议》《招行协议》《中国银行协议》《上海银行协议》"];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"zhifubao://"
                                 range:[[attributedString string] rangeOfString:@"《支付宝协议》"]];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"weixin://"
                                 range:[[attributedString string] rangeOfString:@"《微信协议》"]];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"jianhang://"
                                 range:[[attributedString string] rangeOfString:@"《建行协议》"]];
     
    
        UIImage *image = [UIImage imageNamed:select == YES ? @"new_feature_share_true" : @"new_feature_share_false"];
        CGSize size = CGSizeMake(font + 2, font + 2);
        UIGraphicsBeginImageContextWithOptions(size, false, 0);
        [image drawInRect:CGRectMake(0, 2, size.width, size.height)];
        UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = resizeImage;
        NSMutableAttributedString *imageString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment];
        [imageString addAttribute:NSLinkAttributeName
                                 value:@"checkbox://"
                                 range:NSMakeRange(0, imageString.length)];
        [attributedString insertAttributedString:imageString atIndex:0];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, attributedString.length)];
        _textview.attributedText = attributedString;
        _textview.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
                                         NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                         NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
        
        _textview.delegate = self;
        _textview.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
        _textview.scrollEnabled = NO;
    }
    
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
        if ([[URL scheme] isEqualToString:@"jianhang"]) {
            NSLog(@"建行支付---------------");
            return NO;
        } else if ([[URL scheme] isEqualToString:@"zhifubao"]) {
            NSLog(@"支付宝支付---------------");
            return NO;
        } else if ([[URL scheme] isEqualToString:@"weixin"]) {
           NSLog(@"微信支付---------------");
            return NO;
        } else if ([[URL scheme] isEqualToString:@"checkbox"]) {
            self.isSelect = !self.isSelect;
            [self protocolIsSelect:self.isSelect];
            return NO;
        }
        return YES;
    }
    @end
    

    注:修改按钮的颜色

    _textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    富文本点击事件.png

    相关文章

      网友评论

      • 獨荹儛臨:这个Demo会闪退、如下:长按图片、必现崩溃
      • 娶什么名字呢:能去除掉双击出现的菜单嘛
      • 跳跳虾:👍
        娶什么名字呢:@跳跳虾 那个是你没选框图片的原因吧
        anny_4243:@跳跳虾 可以的,
        -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL
        inRange:(NSRange)characterRange{
        NSRange range = {13,4}; //这里是可点击范围
        if (NSEqualRanges(characterRange, range)) {
        // 这里添加点击事件
        }
        return YES;
        }
        跳跳虾:点击范围能控制吗?点击链接的下一行也能触发击事件
      • 五蕴盛:给你个赞

      本文标题:iOS 富文本添加点击事件

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