TextKit实现图文混排
来源:互联网 发布:现在最流行的网络词语 编辑:程序博客网 时间:2024/05/17 22:45
1. // NSAttributedString 这个类可以设置文本属性:加粗、斜体、删除线、下划线...
2. // NSMutableAttributedString 可变属性文本:可以动态添加文本的属性
3. NSString *text = @"iOS实现图文混排的方式:1.WebView html+javascript 2、CoreText (C语言实现的框架) ";
4. UITextView *textview = [[UITextView alloc]initWithFrame:frame];
5. textview.delegate = self;
6. textview.text = text;
7. NSMutableAttributedString *attrstring = [[NSMutableAttributedStringalloc] initWithString:text];
8.
9. //1.设置字体
10. NSRange rg = [textrangeOfString:@"CoreText"];
11. [attrstring addAttribute:NSFontAttributeName
12. value:[UIFont boldSystemFontOfSize:18]
13. range:rg];
14.//2.设置字体颜色
15. NSString *s2 =@"WebView";
16. [attrstringaddAttribute:NSForegroundColorAttributeName
17. value:[UIColor redColor]
18. range:[text rangeOfString:s2]];
19.//3.设置字体的背景颜色
20. [attrstringaddAttribute:NSBackgroundColorAttributeName
21. value:[UIColor greenColor]
22. range:[text rangeOfString:s2]];
23.textview.attributedText = attrstring;
24.
25.
26.//1.字典中存储多个文本属性
27. NSDictionary*attributes = @{
28. NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
29. NSUnderlineStyleAttributeName:@1
30. };
31.//2.字典中多个文本属性,对整个text字符串设置
32. NSMutableAttributedString *attrstring = [[NSMutableAttributedStringalloc] initWithString:text attributes:attributes];
33.//3.添加超连接
34. NSString *s1 =@"html+javascript";
35. [attrstringaddAttribute:NSLinkAttributeName
36. value:@"http://www.baidu.com"
37. range:[text rangeOfString:s1]];
38.//4.修改超连接的文本属性
39. textview.linkTextAttributes = @{
40. NSForegroundColorAttributeName : [UIColor redColor],
41. NSUnderlineStyleAttributeName:@1
42. };
43.//5.添加(表情)图片
44. @interfaceImgTextAttachment : NSTextAttachment
45. //<1> 创建图片附件
46. ImgTextAttachment*imgTextAtta = [[ImgTextAttachment alloc] init];
47. imgTextAtta.image =[UIImage imageNamed:@"026.gif"];
48.
49. //<2>创建文本属性对象,用于包装图片附件
50. NSAttributedString*imgAttributed = [NSAttributedStringattributedStringWithAttachment:imgTextAtta];
51.
52. //3> 将图片文本,插入到字符串中的某个位置
53. [attrstringinsertAttributedString:imgAttributed atIndex:3];
54.textview.attributedText = attrstring;
55.
56.//UITextView 中的超连接被点击的协议方法
57.- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
58.
59. NSLog(@"%@",URL);
60.
61. //自己处理页面的调整
62.
63. return NO;
64.}
65.
66.补充:
67.//NSAttributedString.h 中文本属性key的说明
68./*
69. NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
70. NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
71. NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
72. NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
73. NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
74. NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
75. NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
76. NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
77. NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
78. NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
79. NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
80. NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
81. NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
82. NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
83. NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
84. NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
85. NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
86. NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
87. NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
88. NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
89. NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
90. */
91.
92.
- TextKit实现图文混排
- TextKit实现图文混排
- TextKit实现图文混排
- 用textkit实现图文混排
- 图文混排(TextKit)
- 图文混排(TextKit详解)
- TextKit简单用法(图文混排)
- textkit相关做图文混排键盘
- 用TextKit实现表情混排
- 用TextKit实现表情混排
- 富文本(图文混排)—— TextKit & CoreText
- 利用TextKit进行简单的图文混排
- 图文混排实现
- CoreText实现图文混排
- CoreText实现图文混排
- CoreText实现图文混排
- CoreText实现图文混排
- webView实现图文混排
- 【读书笔记+小练习】操作系统之进程及其实现
- HTML5安全
- Openwrt的wifi脚本分析
- 四大组件之Activity(二)
- IT学术交流盛会,你不容错过!
- TextKit实现图文混排
- 第六届-蓝桥杯省赛-生命之树
- 分析Linux内核创建一个新进程的过程
- intellij idea 基础配置指导
- Add Two Numbers
- .NET在后置代码中输入JS提示语句(背景不会变白)
- 单链表逆置的实现
- 模式之--抽象工厂(Abstract Factory)未完
- 转载 http://drops.wooyun.org/tips/5462?utm_source=tuicool 看来基本算法还是有地方可用