zoukankan      html  css  js  c++  java
  • iOS--NSAttributedString使用介绍

    iOS–NSAttributedString使用介绍

    原文见:
    http://www.itnose.net/detail/6177538.html
    http://www.csdn123.com/html/topnews201408/92/1692.htm

    转载地址:http://blog.csdn.net/lotheve/article/details/46849873

    字符属性介绍:

    //NSAttributedString的属性:
    // NSFontAttributeName                设置字体属性,取值为 UIFont类型,默认值:字体:Helvetica(Neue)字号:12
    // NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色
    // NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil,透明色
    // NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0表示没有连体字符,1表示使用默认的连体字符
    // NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
    // NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
    // NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
    // NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
    // NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
    // NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
    // NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象
    // NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象
    // NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
    // NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
    // NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
    // NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
    // NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写
    // NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0表示横排文本,1表示竖排文本
    // NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址
    // NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
    // NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象

    字符属性

    字符属性可以应用于 attributedstring 的文本中。另外,可以直接设置UILabel、UITextField、UITextView的富文本属性attributedText

    1. NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

    2. NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色

    3. NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色

    4. NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符。2 表示使用所有连体符号,默认值为 1(iOS 不支持 2)

    5. NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄

    6. NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)

      1.枚举常量 NSUnderlineStyle中的值
      // NSUnderlineStyleNone   不设置删除线
      // NSUnderlineStyleSingle 设置删除线为细单实线
      // NSUnderlineStyleThick  设置删除线为粗单实线
      // NSUnderlineStyleDouble 设置删除线为细双实线
       2.**注意**:虽然使用了枚举常量,但是枚举常量的本质仍为整数,所以同样必须先转化为 NSNumber 才能使用
       3.删除线和下划线使用相同的枚举常量作为其属性值
       4.目前iOS中只有上面列出的4中效果,虽然我们能够在头文件中发现其他更多的取值,但是使用后没有任何效果
       5.另外,删除线属性取值除了上面的4种外,其实还可以取其他整数值,有兴趣的可以自行试验,取值为 0 - 7时,效果为单实线,随着值得增加,单实线逐渐变粗,取值为 9 - 15时,效果为双实线,取值越大,双实线越粗。
      
    7. NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色

    8. NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似

    9. NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色

      NSShadow介绍://NSShadow 对象比较简单,只有3个属性:阴影颜色,模糊半径和偏移
      NSShadow *shadow1 = [[NSShadow alloc] init];  
      shadow1.shadowOffset = CGSizeMake(3, 3);      //阴影偏移(X方向偏移和Y方向偏移)
      shadow1.shadowBlurRadius = 0.5;               //模糊半径
      shadow1.shadowColor = [UIColor orangeColor];  //阴影颜色
      
    10. NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果

    11. NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象

    12. NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象

    13. NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有一个可用的特效

      //NSTextEffectLetterpressStyle(凸版印刷效果),适用于iOS 7.0及以上
      
    14. NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏

    15. NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾

    16. NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本

    17. NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写

      设置文字书写方向,取值为以下组合
       //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
       //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
       //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
       //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
       一直没搞明白  NSTextWritingDirectionEmbedding 和  NSTextWritingDirectionOverride 有什么不同的效果。
      
    18. NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义

    19. NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址

      NSDictionary *attrDict1 = @{ NSLinkAttributeName: [NSURL URLWithString: @"http://www.baidu.com"],
      NSFontAttributeName: [UIFont systemFontOfSize:20]
      };
      
      _textview.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
      _textview.scrollEnabled = NO;   //可选
      _textview.delegate = self;      //必须设置,否则代理函数不会被回调
      
      _textview.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
      代理函数:
      - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
      return YES;
      }
    20. NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

      NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
      textAttachment.image = [UIImage imageNamed: @"10000.jpeg"];  //设置图片源
      textAttachment.bounds = CGRectMake(0, 0, 30, 30);          //设置图片位置和大小(***x,y都不起效***)
      NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString: originStr];
      
      [attrStr addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];
      NSAttributedString *attrStr = [NSAttributedString attributedStringWithAttachment: textAttachment];
      
      [attrStr insertAttributedString: attrStr atIndex: 2];  //NSTextAttachment占用一个字符长度,插入后原字符串长度增加1
      
      _label.attributedText = attrStr;
    21. NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

      // alignment               对齐方式,取值枚举常量 NSTextAlignment
      // firstLineHeadIndent     首行缩进,取值 float
      // headIndent              缩进,取值 float
      // tailIndent              尾部缩进,取值 float
      // ineHeightMultiple       可变行高,乘因数,取值 float
      // maximumLineHeight       最大行高,取值 float
      // minimumLineHeight       最小行高,取值 float
      // lineSpacing             行距,取值 float
      // paragraphSpacing        段距,取值 float
      // paragraphSpacingBefore  段首空间,取值 float
      //    
      // baseWritingDirection    句子方向,取值枚举常量 NSWritingDirection
      // lineBreakMode           断行方式,取值枚举常量 NSLineBreakMode
      // hyphenationFactor       连字符属性,取值 0 - 1

    *NSParagraphStyleAttributeName*

    1. alignment 对齐方式,取值枚举常量 NSTextAlignment

      enum {
          NSTextAlignmentLeft      = 0,
          NSTextAlignmentCenter    = 1,
          NSTextAlignmentRight     = 2,
          NSTextAlignmentJustified = 3,
          NSTextAlignmentNatural   = 4,
      };
    2. firstLineHeadIndent 首行缩进,取值 float

    3. headIndent 缩进,取值 float

    4. tailIndent 尾部缩进,取值 float,注意距离是从行首算起

    5. ineHeightMultiple 可变行高,乘因数,取值 float,大于1行高变大,小于1行高变小,实际上字体大小不会改变,改变的是行间距

    6. maximumLineHeight 最大行高,取值 float,若其值小于默认行高,则行间距变小,若其值大于默认行高,则不会引起任何变化

    7. minimumLineHeight 最小行高,取值 float,若其值大于默认行高,则行间距变大,若其值小于默认行高,则不会引起任何变化

    8. lineSpacing 行距,取值 float,可正可负,正值增加行距,负值减小行距

    9. paragraphSpacing 段距,取值 float,负值无效,取0值

    10. paragraphSpacingBefore 段首空间,取值 float,最小取值为0

    11. baseWritingDirection 句子方向,取值枚举常量NSWritingDirection

          enum {
              NSWritingDirectionNatural = -1,
              NSWritingDirectionLeftToRight =  0,
              NSWritingDirectionRightToLeft =  1
          };
          typedef NSInteger NSWritingDirection;
    12. lineBreakMode 断行方式,取值枚举常量 NSLineBreakMode

          enum {
              NSLineBreakByWordWrapping = 0, //自动换行,单词切断
              NSLineBreakByCharWrapping,     //自动换行,字母切断
              NSLineBreakByClipping,         //非自动换行,不切断
              NSLineBreakByTruncatingHead,   //非自动换行,行首切断
              NSLineBreakByTruncatingTail,   //非自动换行,行尾切断
              NSLineBreakByTruncatingMiddle  //非自动换行,中间切断
          };
          typedef NSUInteger NSLineBreakMode;
    13. hyphenationFactor 连字符属性,取值 0 到 1 之间,开启断词功能

    通过使用boundingRectWithSize, 计算UILable高度

    使用方法:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);

    使用时的注意事项:

    • NSAttributedString 的每个部分都要至少设置两个属性:

      • NSFontAttributeName
      • NSForegroundColorAttributeName
    • NSStringDrawingOptions 的值, 在多行的情况下, 至少要

      • NSStringDrawingUsesLineFragmentOrigin
      • NSStringDrawingUsesFontLeading

    下面是示范代码:

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串测试计算高度字符串1"];
        [string setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(0, string.length)];
        [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 11)];
        NSMutableParagraphStyle *paragraghStyle = [[NSMutableParagraphStyle alloc]init];
        paragraghStyle.lineSpacing = 20;
        [string addAttribute:NSParagraphStyleAttributeName value:paragraghStyle range:NSMakeRange(0, string.length)];
        CGSize size = [string boundingRectWithSize:CGSizeMake(200, 500) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    
        UILabel *label = [[UILabel alloc]init];
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.backgroundColor = [UIColor redColor];
        label.attributedText = string;
        label.frame = CGRectMake(0, 100, size.width, size.height);
        [self.view addSubview:label];

    效果图为:

  • 相关阅读:
    MYSQL进阶学习笔记十:MySQL慢查询!(视频序号:进阶_23-24)
    MYSQL进阶学习笔记九:MySQL事务的应用!(视频序号:进阶_21-22)
    MYSQL学习拓展一:MySQL 存储过程之游标的使用!
    MYSQL进阶学习笔记八:MySQL MyISAM的表锁!(视频序号:进阶_18-20)
    linux初级学习笔记十:linux grep及正则表达式!(视频序号:04_4)
    linux初级学习笔记九:linux I/O管理,重定向及管道!(视频序号:04_3)
    MYSQL进阶学习笔记七:MySQL触发器的创建,应用及管理!(视频序号:进阶_16,17)
    linux shell 字符串操作
    How to Install JAVA 8 (JDK/JRE 8u111) on Debian 8 & 7 via PPA
    iptables Data filtering详解
  • 原文地址:https://www.cnblogs.com/AbeDay/p/5026898.html
Copyright © 2011-2022 走看看