- // NSAttributedString 這個類可以設置文本屬性:加粗、斜體、刪除線、下划線...
- // NSMutableAttributedString 可變屬性文本:可以動態添加文本的屬性
- NSString *text = @"iOS實現圖文混排的方式:1.WebView html+javascript 2、CoreText (C語言實現的框架) ";
- UITextView *textview = [[UITextView alloc] initWithFrame:frame];
- textview.delegate = self;
- textview.text = text;
- NSMutableAttributedString *attrstring = [[NSMutableAttributedString alloc] initWithString:text];
- //1.設置字體
- NSRange rg = [text rangeOfString:@"CoreText"];
- [attrstring addAttribute:NSFontAttributeName
- value:[UIFont boldSystemFontOfSize:18]
- range:rg];
14.//2.設置字體顏色
- NSString *s2 = @"WebView";
- [attrstring addAttribute:NSForegroundColorAttributeName
- value:[UIColor redColor]
- range:[text rangeOfString:s2]];
19.//3.設置字體的背景顏色
- [attrstring addAttribute:NSBackgroundColorAttributeName
- value:[UIColor greenColor]
- range:[text rangeOfString:s2]];
23.textview.attributedText = attrstring;
26.//1.字典中存儲多個文本屬性
- NSDictionary *attributes = @{
- NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
- NSUnderlineStyleAttributeName:@1
- };
31.//2.字典中多個文本屬性,對整個text字符串設置
- NSMutableAttributedString *attrstring = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
33.//3.添加超連接
- NSString *s1 = @"html+javascript";
- [attrstring addAttribute:NSLinkAttributeName
- value:@"http://www.baidu.com"
- range:[text rangeOfString:s1]];
38.//4.修改超連接的文本屬性
- textview.linkTextAttributes = @{
- NSForegroundColorAttributeName : [UIColor redColor],
- NSUnderlineStyleAttributeName:@1
- };
43.//5.添加(表情)圖片
- @interface ImgTextAttachment : NSTextAttachment
- //<1> 創建圖片附件
- ImgTextAttachment *imgTextAtta = [[ImgTextAttachment alloc] init];
- imgTextAtta.image = [UIImage imageNamed:@"026.gif"];
- //<2>創建文本屬性對象,用於包裝圖片附件
- NSAttributedString *imgAttributed = [NSAttributedString attributedStringWithAttachment:imgTextAtta];
- //3> 將圖片文本,插入到字符串中的某個位置
- [attrstring insertAttributedString:imgAttributed atIndex:3];
54.textview.attributedText = attrstring;
56.//UITextView 中的超連接被點擊的協議方法
57.- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
- NSLog(@"%@",URL);
- //自己處理頁面的調整
- return NO;
64.}
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. */