通过YYtext实现文本点击(类似微博效果)
2016-07-20 21:37
543 查看
最近想在文本中实现其中一段文字的点击事件,进行超链接,自己琢磨了很久,很遗憾最后还是没有通过自己实现出来,在网上查找一番,发现YYtext这个三方是专门为文字类所使用,经过一番了解,发现其中正好有我所需要的,很简单
导入YYtext后 直接调用即可
代码如下:
NSMutableAttributedString *text = [[NSMutableAttributedString
alloc]
initWithString:@"Some Text, blabla..."];
text.yy_font = [UIFont
boldSystemFontOfSize:13.0f];
text.yy_color = [UIColor
blueColor];
[text yy_setColor:[UIColor
redColor] range:NSMakeRange(0,
4)];
[text yy_setTextHighlightRange:NSMakeRange(0,
4)//设置点击的位置
color:[UIColor
orangeColor]
backgroundColor:[UIColor
whiteColor]
tapAction:^(UIView *containerView,
NSAttributedString *text,
NSRange range, CGRect rect){
NSLog(@"这里是点击事件");
}];
YYLabel *heightRangeLabel = [YYLabel
new];
heightRangeLabel.frame =
CGRectMake(100,
250, 160,
25);
heightRangeLabel.attributedText = text;
heightRangeLabel.userInteractionEnabled =
YES;
heightRangeLabel.backgroundColor = [UIColor
whiteColor];
[self.view
addSubview:heightRangeLabel];
导入YYtext后 直接调用即可
代码如下:
NSMutableAttributedString *text = [[NSMutableAttributedString
alloc]
initWithString:@"Some Text, blabla..."];
text.yy_font = [UIFont
boldSystemFontOfSize:13.0f];
text.yy_color = [UIColor
blueColor];
[text yy_setColor:[UIColor
redColor] range:NSMakeRange(0,
4)];
[text yy_setTextHighlightRange:NSMakeRange(0,
4)//设置点击的位置
color:[UIColor
orangeColor]
backgroundColor:[UIColor
whiteColor]
tapAction:^(UIView *containerView,
NSAttributedString *text,
NSRange range, CGRect rect){
NSLog(@"这里是点击事件");
}];
YYLabel *heightRangeLabel = [YYLabel
new];
heightRangeLabel.frame =
CGRectMake(100,
250, 160,
25);
heightRangeLabel.attributedText = text;
heightRangeLabel.userInteractionEnabled =
YES;
heightRangeLabel.backgroundColor = [UIColor
whiteColor];
[self.view
addSubview:heightRangeLabel];
相关文章推荐
- iOS开发学习之YYKit中YYText的深入解析,YYTextShadow的代码解析
- 富文本点击事件-TTTAttributedLabel和YYtext的不同用法
- android textview等文本内容中字体颜色的改变和字体背景色的改变
- iOS开发之YYKit丰富的组件,如:YYText
- YYText学习之根据range设置text的颜色和边框
- linux运维学习决心书
- Dwz/Jquery--使用Ajax提交表单时调用表单设置的校验
- 挑7
- Spring AOP的底层实现技术---JDK动态代理
- jQuery Mobile 表单基础
- I - the Sum of Cube
- Python文件处理
- poj1724【最短路】
- 控制TextView滚动
- [软件人生]关于公积金的用途
- 匈牙利算法来找最多的没有弱化的!!!!但是初始化那块不知道为什么换了个地方就re。。。尴尬
- Angular2 架构
- MyEclipse:(4)MyEclipse中怎么设置Add cast to Clazz 快捷键
- apply, call, bind在js中的区别
- Tiny6410学习—Linux命令