利用NSAttributeString进行图文混编
2016-04-05 15:07
267 查看
NSString* showText = [NSString stringWithFormat:@"%@%@",text1,text2]; NSRange range1 = [showText rangeOfString:text1]; NSRange range2 = [showText rangeOfString:text2]; NSDictionary* attributes1 = @{ NSFontAttributeName: font, NSForegroundColorAttributeName:dColor }; NSDictionary* attributes2 = @{ NSFontAttributeName: font, NSForegroundColorAttributeName:cColor }; NSMutableAttributedString* attributeString = [[[NSMutableAttributedString alloc] initWithString:showText] autorelease]; [attributeString addAttributes:attributes1 range:range1]; [attributeString addAttributes:attributes2 range:range2]; NSTextAttachment* textAttachment = [[[NSTextAttachment alloc] init] autorelease]; textAttachment.image = image; textAttachment.bounds = CGRectMake(0, -4, 16, 16); // 微调图片位置 NSAttributedString* imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment]; [attributeString insertAttributedString:imageAttachment atIndex:range1.length]; // 插入图片 label.attributedText = attributeString;
相关文章推荐
- Python:eval的妙用和滥用
- 通讯录
- C#StreamReader和StreamWriter
- Page.IsValid的使用
- CSS学习笔记(一)
- ProgressBar AsyncTask实现界面数据异步加载
- Twitter 工程师谈 JVM 调优
- JavaScript的运算符
- 10个超级有用、必须收藏的PHP代码样例
- POP3、SMTP和IMAP之间的区别和联系
- Tomcat学习总结
- 使用brew安装软件
- CSS
- 【新人笔记16.04.05】JS--事件绑定
- NDK-JNI实战教程(三) 从比Hello World稍复杂点儿的NDK例子说说模板
- shell脚本中for循环后面冒号的问题
- 使用ab进行页面的压力测试
- 杨辉三角问题
- 理解 HTTPS 的工作原理
- ThreadLocal介绍