美文网首页
16-NSAttributedString

16-NSAttributedString

作者: ForstDragon | 来源:发表于2019-06-18 16:23 被阅读0次

1,属性介绍

NSAttributedString 共有21个属性

// 上面例子中用过这个属性,字体 - 默认字体:Helvetica(Neue),字号:12
1. NSFontAttributeName

// 段落 - 取值为 NSParagraphStyle 对象,文本段落排版格式,行间距等
2. NSParagraphStyleAttributeName

// 字体颜色 - 默认为黑色
3. NSForegroundColorAttributeName

// 字体背景颜色 - 默认为无背景色
4. NSBackgroundColorAttributeName

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

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

// 删除线 - 取值为 NSNumber 对象(整数)
7. NSStrikethroughStyleAttributeName

// 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
8. NSStrikethroughColorAttributeName 

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

// 下划线颜色,UIColor 对象,默认值为黑色
10. NSUnderlineColorAttributeName  

// 笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
11. NSStrokeWidthAttributeName 

// 填充部分颜色,不是字体颜色,取值为
12. NSStrokeColorAttributeName

// 阴影属性,取值为 NSShadow 对象
13. NSShadowAttributeName 

// 文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
14. NSTextEffectAttributeName 

// 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
15. NSBaselineOffsetAttributeName 

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

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

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

// 文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
19. NSVerticalGlyphFormAttributeName

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

// 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
21. NSAttachmentAttributeName

例子1:描边

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


描边.png
    1,
    NSString *_text = @"Hi_Dammon";
    NSDictionary *dict =@{
                          NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                          NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                          NSForegroundColorAttributeName : [UIColor redColor],
                          NSStrokeWidthAttributeName: @3,
                          NSStrokeColorAttributeName : [UIColor greenColor],
                          };
    [_text drawInRect:self.bounds withAttributes:dict]
  2, 
    NSString *_text = @"Hi_Dammon";
    NSDictionary *dict =@{
                          NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                          NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                          NSForegroundColorAttributeName : [UIColor redColor],
                          NSStrokeWidthAttributeName: @3,
                          NSStrokeColorAttributeName : [UIColor greenColor],
                          };
    [_text drawInRect:self.bounds withAttributes:dict]

添加删除线,NSStrikethroughStyleAttributeName

NSDictionary *dict =@{
                          NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                          NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                          NSForegroundColorAttributeName : [UIColor redColor],
                          NSStrikethroughColorAttributeName:@(NSUnderlineStyleSingle),
                          };

NSUnderlineStyleAttributeName 添加下划线

NSDictionary *dict =@{
                          NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                          NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                          NSForegroundColorAttributeName : [UIColor redColor],
                          NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                          };

NSShadowAttributeName 设置阴影,单独设置不好使,必须和其他属性搭配才好使。

NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5;//模糊度
shadow.shadowColor  = [UIColor blueColor];
shadow.shadowOffset = CGSizeMake(1, 3);
    
    
NSDictionary *dict =@{
                   NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                   NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                   NSForegroundColorAttributeName : [UIColor redColor],
                   NSShadowAttributeName: shadow,
                    //该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义
                   NSVerticalGlyphFormAttributeName:@(0),
               };

//字体倾斜
NSDictionary *dict =@{
                   NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                   NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                   NSForegroundColorAttributeName : [UIColor redColor],
                   NSShadowAttributeName: shadow,
                    //该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义
                   NSVerticalGlyphFormAttributeName:@(0),
                    //字体倾斜
                   NSObliquenessAttributeName:@(1),
               };
//字体扁平化
NSDictionary *dict =@{
                   NSFontAttributeName : [UIFont systemFontOfSize:15.0],
                   NSParagraphStyleAttributeName :[[NSMutableParagraphStyle alloc] init],
                   NSForegroundColorAttributeName : [UIColor redColor],
                   NSShadowAttributeName: shadow,
                    //该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义
                   NSVerticalGlyphFormAttributeName:@(0),
                    //字体扁平化
                   NSExpansionAttributeName:@(1),
               };

相关文章

  • 16-NSAttributedString

    1,属性介绍 例子1:描边 NSStrokeWidthAttributeName这个属性所对应的值是一个 NSNu...

网友评论

      本文标题:16-NSAttributedString

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