富文本Attributes详解

9 篇文章 0 订阅
1 篇文章 0 订阅

先看图-如何做的呢?
示例图
大概一眼就能明白这篇干货的目的:文本属性Attributes


1.范例代码

话不多说,直接上代码

UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(30, 60, 500, 40)];
NSDictionary * attributes = @{NSForegroundColorAttributeName:[UIColor redColor],
                              NSBackgroundColorAttributeName:[UIColor greenColor],
                                         NSFontAttributeName:[UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:19.0],
                                         NSKernAttributeName:@1.0};
NSString * string = @"This is an attributes string";
NSAttributedString * attributeText = [[NSAttributedString alloc]initWithString:string attributes:attributes];
[label1 setAttributedText:attributeText];
[self.view addSubview:label1];

UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 300, 40)];
NSDictionary * attributes2 = @{NSBackgroundColorAttributeName:[UIColor orangeColor],
                                          NSFontAttributeName:[UIFont fontWithName:@"ArialUnicodeMS" size:19.0],
                                          NSKernAttributeName:@5.0};
NSString * string2 = @"Orange Background";
NSAttributedString * attributeText2 = [[NSAttributedString alloc]initWithString:string2 attributes:attributes2];
[label2 setAttributedText:attributeText2];
[self.view addSubview:label2];

2.Attributes属性

进入UIKitNSAttributedString.hAttributes属性

/** 系统提供了这么多 */
NSFontAttributeName                   // 设置字体
NSParagraphStyleAttributeName         // 设置段落风格
NSForegroundColorAttributeName        // 设置文字颜色
NSBackgroundColorAttributeName        // 设置背景颜色
NSLigatureAttributeName               // 设置连体属性
NSKernAttributeName                   // 设置字符间距
NSStrikethroughStyleAttributeName     // 添加删除线
NSUnderlineStyleAttributeName         // 添加下划线
NSStrokeColorAttributeName            // 设置文字描边颜色
NSStrokeWidthAttributeName            // 设置文字描边宽度
NSShadowAttributeName                 // 设置阴影
NSTextEffectAttributeName             // 设置文本特殊效果
NSAttachmentAttributeName             // 设置文本附件
NSLinkAttributeName                   // 设置链接属性
NSBaselineOffsetAttributeName         // 设置基线偏移量
NSUnderlineColorAttributeName         // 添加下划线颜色
NSStrikethroughColorAttributeName     // 添加删除线颜色
NSObliquenessAttributeName            // 设置字体倾斜
NSExpansionAttributeName              // 设置文本扁平
NSWritingDirectionAttributeName       // 设置文字书写方向
NSVerticalGlyphFormAttributeName      // 设置文本段落排版格式

我们常把需要的属性放在字典中添加给字符串,使用方法看范例代码
下面我们看看这些属性都干了什么,常用的有:

1.NSFontAttributeName 设置字体

NSFontAttributeName:[UIFont systemFontOfSize:(CGFloat)] // 字体大小
NSFontAttributeName:[UIFont fontWithName:(nonnull NSString *) size:(CGFloat)] // 字体名称,字体大小

2.NSParagraphStyleAttributeName 设置段落风格

3.NSForegroundColorAttributeName、NSBackgroundColorAttributeName设置字体和背景颜色

NSForegroundColorAttributeName:[UIColor redColor]
NSBackgroundColorAttributeName:[UIColor greenColor]

4.NSKernAttributeName 设置字符间距

正值间距加宽,负值间距变窄

NSKernAttributeName:@-1.0

5.NSStrikethroughStyleAttributeName、NSUnderlineStyleAttributeName添加删除线和下划线

NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)
NSStrikethroughColorAttributeName:[UIColor redColor]
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)
NSUnderlineColorAttributeName:[UIColor redColor]

6.NSStrokeColorAttributeName、NSStrokeWidthAttributeName 设置文字描边颜色和宽度

/** 单独设置颜色无效果,需和描边宽度同时设置 */
NSStrokeColorAttributeName:[UIColor redColor]
NSStrokeWidthAttributeName:@3

7.NSShadowAttributeName 设置阴影

官方文档说明NSShadowAttributeName默认为空,需要一个NSShadow实例对象。

NSShadow * shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5; // 模糊度
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(1, 3);
/** 使用时 */
NSShadowAttributeName:shadow

效果
NSShadow
NSShadowAttributeName也常和NSVerticalGlyphFormAttributeNameNSObliquenessAttributeNameNSExpansionAttributeName结合使用

8.NSVerticalGlyphFormAttributeName、NSObliquenessAttributeName、NSExpansionAttributeName绘制文本、设置字体倾斜、设置文本横向拉伸压缩属性

对于NSVerticalGlyphFormAttributeName设置文本排版格式,0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。

NSVerticalGlyphFormAttributeName:@(0)
NSObliquenessAttributeName:@1
NSExpansionAttributeName:@1

9.NSLigatureAttributeName 设置连体属性

0 表示没有连体字符,1 表示使用默认的连体字符,2 表示使用所有连体符号,默认值为 1(iOS 不支持 2)
Zapfino字体下:

NSLigatureAttributeName:@0,
NSLigatureAttributeName:@1,

自找亮点:
NSLigatureAttributeName1
NSLigatureAttributeName2

10.NSTextEffectAttributeName 设置文本特殊效果

取值为 NSString 对象,目前只有图版印刷效果可用

NSTextEffectAttributeName: NSTextEffectLetterpressStyle

上面是添加属性的label,下面是未添加属性的label
NSTextEffectAttributeName

11.NSLinkAttributeName 设置链接属性

点击后调用浏览器打开指定URL地址

NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]

12.NSBaselineOffsetAttributeName 设置基线偏移量

正值上偏,负值下偏

NSBaselineOffsetAttributeName:@3

13.NSAttachmentAttributeName 设置文本附件

取值为NSTextAttachment对象,常用于文字图片混排

NSAttachmentAttributeName:id

14.NSWritingDirectionAttributeName 设置文字书写方向

从左向右书写或者从右向左书写,取值:

@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]

使用:

NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)]

NSWritingDirectionAttributeName


3.UIFont字体选择

创建并且返回一个指定字体类型和大小的字体对象,fontName指定字体集的名字和风格(例如宋体,楷书等这一类型的),fontsize设置字体的大小。

+ (nullable UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize;  
/* 字体名如下 */
Font Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold

Font Family: AppleGothic
Font: AppleGothic

Font Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT

Font Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold

Font Family: Arial Unicode MS
Font: ArialUnicodeMS

Font Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold

Font Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT

Font Family: DB LCD Temp
Font: DBLCDTempBlack

Font Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic

Font Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold

Font Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold

Font Family: Hiragino Kaku Gothic **** W3
Font: HiraKakuProN-W3

Font Family: Hiragino Kaku Gothic **** W6
Font: HiraKakuProN-W6

Font Family: Marker Felt
Font: MarkerFelt-Thin

Font Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light

Font Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light

Font Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light

Font Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium

Font Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT

Font Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold

Font Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic

Font Family: Zapfino
Font: Zapfino
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值