TextKit實現圖文混排


  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 = [[NSMutableAttributedString alloc] initWithString:text];
  8.  
  9. //1.設置字體
  10. NSRange rg = [text rangeOfString:@"CoreText"];
  11. [attrstring addAttribute:NSFontAttributeName
  12. value:[UIFont boldSystemFontOfSize:18]
  13. range:rg];

14.//2.設置字體顏色

  1. NSString *s2 = @"WebView";
  2. [attrstring addAttribute:NSForegroundColorAttributeName
  3.               value:[UIColor redColor]
  4. range:[text rangeOfString:s2]];

19.//3.設置字體的背景顏色

  1. [attrstring addAttribute:NSBackgroundColorAttributeName
  2. value:[UIColor greenColor]
  3.  range:[text rangeOfString:s2]];

23.textview.attributedText = attrstring;

26.//1.字典中存儲多個文本屬性

  1. NSDictionary *attributes = @{
  2. NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
  3. NSUnderlineStyleAttributeName:@1
  4. };

31.//2.字典中多個文本屬性,對整個text字符串設置

  1. NSMutableAttributedString *attrstring = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];

33.//3.添加超連接

  1. NSString *s1 = @"html+javascript";
  2. [attrstring addAttribute:NSLinkAttributeName
  3. value:@"http://www.baidu.com"
  4. range:[text rangeOfString:s1]];

38.//4.修改超連接的文本屬性

  1. textview.linkTextAttributes = @{
  2. NSForegroundColorAttributeName : [UIColor redColor],
  3. NSUnderlineStyleAttributeName:@1
  4. };

43.//5.添加(表情)圖片

  1. @interface ImgTextAttachment : NSTextAttachment
  2. //<1> 創建圖片附件
  3. ImgTextAttachment *imgTextAtta = [[ImgTextAttachment alloc] init];
  4. imgTextAtta.image = [UIImage imageNamed:@"026.gif"];
  5. //<2>創建文本屬性對象,用於包裝圖片附件
  6. NSAttributedString *imgAttributed = [NSAttributedString attributedStringWithAttachment:imgTextAtta];
  7. //3> 將圖片文本,插入到字符串中的某個位置
  8. [attrstring insertAttributedString:imgAttributed atIndex:3];

54.textview.attributedText = attrstring;

56.//UITextView 中的超連接被點擊的協議方法

57.- (BOOL)textView:(UITextView *)textView  shouldInteractWithURL:(NSURL *)URL  inRange:(NSRange)characterRange {

  1. NSLog(@"%@",URL);
  2. //自己處理頁面的調整
  3. 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. */


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM