iOS 字符中插入按钮(简单图文混排)
来源:互联网 发布:matlab 仿射变换矩阵 编辑:程序博客网 时间:2024/05/21 18:36
需求
字符串中某段区域需要添加点击效果或者字符串中添加可点击的按钮。
解决:给UILabel添加点击事件的category。
调用方法
用字符串标记需要点击的位置
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:@"这是一串可点击的字符串,通过字符串进行标记了点击"]; [attributedString1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, attributedString1.length)]; [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5, 2)]; [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(attributedString1.length - 2, 2)]; _textLabel1.attributedText = attributedString1; [_textLabel1 addAttributeActionWithStrings:@[@"点击",@"点击"] tapTargetAction:^(NSString *string, NSRange range, NSInteger index) { NSLog(@"点击字符串:%@ 范围在%@,第%ld个",string,NSStringFromRange(range),index+1); }];
用范围标记需要点击的位置(实现插入按钮)
NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc]initWithString:@"使用范围来进行标记的字符串,可以在字符串在添加图片进行点击,实现再字符串中插入按钮的效果,有颜色的都可点击"]; NSRange rang1 = [attributedString2.string rangeOfString:@"标记"]; NSRange rang2 = [attributedString2.string rangeOfString:@"点击"]; NSRange rang3 = [attributedString2.string rangeOfString:@"按钮"]; [attributedString2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attributedString2.length)]; [attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:rang1]; [attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:rang2]; //添加图片附件(插入图片,添加点击事件) NSTextAttachment *attach = [[NSTextAttachment alloc] init]; attach.image = [UIImage imageNamed:@"abc.jpg"]; attach.bounds = CGRectMake(0, 0, 15, 15); NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach]; [attributedString2 insertAttributedString:attachString atIndex:rang3.location]; _textLabel2.attributedText = attributedString2; [_textLabel2 addAttributeActionWithRange:[NSArray arrayWithObjects:[NSValue valueWithRange:rang1],[NSValue valueWithRange:rang2],[NSValue valueWithRange:rang3],nil] tapTargetAction:^(NSString *string, NSRange range, NSInteger index) { NSLog(@"点击字符串:%@ 范围在%@,第%ld个",string,NSStringFromRange(range),index+1); }];
代码下载
github下载
阅读全文
1 0
- iOS 字符中插入按钮(简单图文混排)
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS实现简单图文混排效果
- iOS 简单图文混排01
- ios图文混排简单演示
- iOS实现简单图文混排效果
- iOS图文混排的简单实现
- ios简单的图文混排
- iOS UILabel中图文混排
- redis主从+哨兵模式
- windows环境下搭建zookeeper伪集群
- python数据分析基础4_参考博文
- Nicescroll滚动条插件的用法
- Tensorflow的模型保存和读取tf.train.Saver
- iOS 字符中插入按钮(简单图文混排)
- CSRF 背景与介绍
- redis代理集群(Twemproxy)(1)
- Tickets HDU
- 单个JVM下支撑100w线程数
- Struts2笔记15 Struts2标签库
- 关于ssh框架中hibernate扫描实体类的两种方法annotatedClasses和packagesToScan
- opencv 轮廓处理与切割
- python加载模块PyImport_ImportModule返回NULL