分享

TextKit实现图文混排

 睁开眼就变帅 2016-08-10
  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. */

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多