美文网首页
富文本NSAttributedString

富文本NSAttributedString

作者: lltree | 来源:发表于2016-08-25 10:33 被阅读38次

    属性介绍

    NSFontAttributeName字体大小

    NSForegroundColorAttributeName字体颜色

    NSBackgroundColorAttributeName字体背景颜色

    UIKIT_EXTERN NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0);                
    // UIFont, default Helvetica(Neue) 12 默认12号  Helvetica 字体
    UIKIT_EXTERN NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     
    // UIColor, default blackColor 默认黑色
    UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     
    // UIColor, default nil: no background 背景色默认没有
    

    测试代码

        NSString *testStr =[NSString stringWithFormat:@"我是测试数据"];
        NSDictionary *dict =@{ NSFontAttributeName:[UIFont systemFontOfSize:17]//字体大小
                              ,NSForegroundColorAttributeName:[UIColor redColor]//字体颜色
                              ,NSBackgroundColorAttributeName:[UIColor greenColor]//字体背景颜色
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    显示结果:


    NSLigatureAttributeName(连体字符)

    UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0);            
    // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
    //
    

    该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。

    NSKernAttributeName字符间距

    UIKIT_EXTERN NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0);                
    // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
    

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

        NSString *testStr =[NSString stringWithFormat:@"hello worlid"];
        NSDictionary *dict =@{
                              NSKernAttributeName:@(5)
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    显示效果

    NSStrikethroughStyleAttributeName(删除线)

    NSStrikethroughColorAttributeName(删除线颜色)

    UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0);  
    // NSNumber containing integer, default 0: no strikethrough
    // NSNumber类型 默认值0 没有删除线
    UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0);  
    // UIColor, default nil: same as foreground color
    

    NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:

    NSUnderlineStyleNone 不设置删除线
    NSUnderlineStyleSingle 设置删除线为细单实线
    NSUnderlineStyleThick 设置删除线为粗单实线
    NSUnderlineStyleDouble 设置删除线为细双实线
    默认值是NSUnderlineStyleNone。

    测试代码:

        NSDictionary *dict =@{
                              NSStrikethroughStyleAttributeName:@(2)//删除线
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    显示效果:

    NSUnderlineStyleAttributeName(下划线)

    NSUnderlineColorAttributeName下划线颜色)

    UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0);     
     // NSNumber containing integer, default 0: no underline
    // NSNumber类型 默认值0 没有下划线
    UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0);      
    // UIColor, default nil: same as foreground color
    // 下划线颜色,如果为nil则等同字体颜色
    

    代码示例:

        NSString *testStr =[NSString stringWithFormat:@"hello worlid"];
        NSDictionary *dict =@{
                              NSStrikethroughStyleAttributeName:@(2),//删除线
                              NSUnderlineStyleAttributeName:@(2)//下划线
                              NSUnderlineColorAttributeName:[UIColor blackColor],//下划线颜色
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    显示效果:


    NSStrokeColorAttributeName(边线颜色)

    NSStrokeWidthAttributeName(边线宽度)

    UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0);         
    // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0);         
    // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
    

    设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.

    NSStrokeWidthAttributeName 这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变笔画宽度(相对于字体 size 的百分比),负值填充效果,正值中空效果,默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为 3.0。
    同时设置了空心的两个属性,并且 NSStrokeWidthAttributeName 属性设置为整数,文字前景色就无效果了

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

    测试代码:

        NSString *testStr =[NSString stringWithFormat:@"hello worlid"];
        NSDictionary *dict =@{
                              NSStrokeColorAttributeName:[UIColor redColor],//边线颜色
                              NSStrokeWidthAttributeName:@(2),//边线宽度
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    显示效果:


    NSShadowAttributeName(阴影)

    UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0);              
    // NSShadow, default nil: no shadow
    // 默认没有阴影
    
       //阴影
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowBlurRadius = 5;//模糊度
        shadow.shadowColor = [UIColor blueColor];
        shadow.shadowOffset = CGSizeMake(1, 3);
        
        NSString *testStr =[NSString stringWithFormat:@"hello worlid"];
        NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字体大小
                              NSShadowAttributeName:shadow,//阴影
                              };
        
        NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:dict];
        
        self.testLable.attributedText = attrStr;
    

    展示效果:


    NSTextEffectAttributeName(凸版印刷体)

    UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0);          
    // NSString, default nil: no text effect
    

    代码示例

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                               NSTextEffectAttributeName:NSTextEffectLetterpressStyle,//凸版印刷体
                              };
        
    

    展示效果

    NSAttachmentAttributeName(文本附件)

    
    

    NSLinkAttributeName(给文本添加链接)

    
    

    NSBaselineOffsetAttributeName(基准线偏移)

    UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0);      
    // NSNumber containing floating point value, in points; offset from baseline, default 0
    //NSNumber类型 默认值0,
    

    代码示例:

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                              NSBaselineOffsetAttributeName:@(15),
                              };
    

    展示效果:

    NSObliquenessAttributeName(字体倾斜)

    UIKIT_EXTERN NSString * const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0);        
     // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
    

    代码示例

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                              NSObliquenessAttributeName:@(1),//字体倾斜
                              };
    

    展示效果:

    NSExpansionAttributeName (文本扁平化)

    UIKIT_EXTERN NSString * const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0);          
    // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
    

    代码示例:

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                              NSExpansionAttributeName:@(1),//文本扁平化
                              };
    self.testLable.numberOfLines = 2;//显示两行
    

    运行效果:

    NSWritingDirectionAttributeName(文本书写方方向)

    UIKIT_EXTERN NSString * const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0);    
    // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  
    The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values. 
    LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, 
    RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding,
    LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, 
    RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
    

    //NSWritingDirectionAttributeName 设置文字书写方向,取值为以下组
    //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
    //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
    //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
    //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]

    示例代码一:

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                              NSWritingDirectionAttributeName:@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)],//书写方向
                              };
    
    

    运行效果一:

    示例代码二:

      NSDictionary *dict =@{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],//字
                              NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)],//书写方向
                              };
    
    

    运行效果二:

    NSVerticalGlyphFormAttributeName(文本显示方向)

    UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0);  
    // An NSNumber containing an integer value.  
    0 means horizontal text.  1 indicates vertical text.  
    0 水平显示 1 竖直显示
    If not specified, it could follow higher-level vertical orientation settings.  
    如果没指定则水平显示
    Currently on iOS, it's always horizontal.  
    当前iOS一直水平显示
    The behavior for any other value is undefined.
    其他值无效
    

    相关文章

      网友评论

          本文标题:富文本NSAttributedString

          本文链接:https://www.haomeiwen.com/subject/hrqosttx.html