当前位置: 首页 > news >正文

富文本 文字图片点击,(TextView)

textview上的富文本支持 文字,图片的点击事件

- (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;
}

在简书看到的,来源于:http://www.jianshu.com/p/480db0cc7380

转载于:https://www.cnblogs.com/liuwenqiang/p/7126132.html

相关文章:

  • 好书分享 ——《深度工作》
  • Linux grep命令
  • oracle with
  • 图片选择器ImageEditContainer
  • ETL基本概念
  • python学习笔记5-python的函数
  • oracle物化视图(materialized view)
  • 绝世好题bzoj4300
  • 在存储过程中拥有role的权限(Authid Current_User)
  • 渐变维度(Slowly Changing Dimension)及其处理方法
  • 获取url值
  • 1.Angular框架-angular介绍与基本使用,MVC模式介绍
  • Oracle性能调优-优化排序操作
  • 鲜为人知的软件项目管理原则
  • 业界经典语录
  • 【EOS】Cleos基础
  • k8s如何管理Pod
  • Linux链接文件
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • vue 配置sass、scss全局变量
  • XForms - 更强大的Form
  • 给初学者:JavaScript 中数组操作注意点
  • 记录一下第一次使用npm
  • 聚簇索引和非聚簇索引
  • 两列自适应布局方案整理
  • 目录与文件属性:编写ls
  • 扑朔迷离的属性和特性【彻底弄清】
  • 如何利用MongoDB打造TOP榜小程序
  • 使用 @font-face
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • python最赚钱的4个方向,你最心动的是哪个?
  • ​configparser --- 配置文件解析器​
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #pragma预处理命令
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (31)对象的克隆
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (接口封装)
  • (算法二)滑动窗口
  • (转)socket Aio demo
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • @FeignClient注解,fallback和fallbackFactory
  • @private @protected @public
  • [AIGC] Nacos:一个简单 yet powerful 的配置中心和服务注册中心
  • [ai笔记9] openAI Sora技术文档引用文献汇总
  • [AX]AX2012 SSRS报表Drill through action
  • [C#]winform制作圆形进度条好用的圆环圆形进度条控件和使用方法