TextAttributes 使得组合属性化字符串变得轻而易举。
let attrs = TextAttributes()
.font(name: "HelveticaNeue", size: 16)
.foregroundColor(white: 0.2, alpha: 1)
.lineHeightMultiple(1.5)
NSAttributedString("The quick brown fox jumps over the lazy dog", attributes: attrs)
特性
- 强类型属性
- 可链式设置方法
- 直接访问
NSParagraphStyle
属性 - 更好的自动完成
用法
- 获取或设置
TextAttributes
属性
attrs.font = UIFont(name: "HelveticaNeue", size: 16)
attrs.backgroundColor = .white
查看 所有属性
- 方法链式调用
TextAttributes
方法返回 Self
以允许方法链式调用
attrs
.lineHeightMultiple(1.5)
.underlineStyle(.styleSingle)
查看 所有方法
- 方法也是构造函数
以下内容是等价的
attrs
.font(name: "HelveticaNeue", size: 16)
.foregroundColor(white: 0.2, alpha: 1)
let font = UIFont(name: "HelveticaNeue", size: 16)
let color = UIColor(white: 0.2, alpha: 1)
attrs
.font(font)
.foregroundColor(color)
- 访问底层字典
attrs.dictionary // Returns the attributes dictionary of type [NSAttributedString.Key: Any]
第三方库
- muukii/TextAttributesUtil 使用TextAttributes快速创建NSAttributedString
演示
使用 Injection for Xcode 进行实时更新
参考
属性字典
字典键 | TextAttributes 属性 |
TextAttributes 方法 |
---|---|---|
NSFontAttributeName |
font |
font(name:size:) |
font(_:) |
||
NSParagraphStyleAttributeName |
paragraphStyle |
paragraphStyle(_:) |
NSForegroundColorAttributeName |
foregroundColor |
foregroundColor(_:) |
foregroundColor(white:alpha:) |
||
foregroundColor(hue:saturation:brightness:alpha:) |
||
foregroundColor(red:green:blue:alpha:) |
||
foregroundColor(patternImage:) |
||
NSBackgroundColorAttributeName |
backgroundColor |
backgroundColor!(:) |
backgroundColor(white:alpha:) |
||
backgroundColor(hue:saturation:brightness:alpha:) |
||
backgroundColor(red:green:blue:alpha:) |
||
backgroundColor(patternImage:) |
||
NSLigatureAttributeName |
ligature |
ligature(_:) |
NSStrikethroughStyleAttributeName |
kern |
kern(_:) |
NSStrikethroughStyleAttributeName |
strikethroughStyle |
strikethroughStyle(_:) |
NSStrikethroughColorAttributeName |
strikethroughColor |
strikethroughColor(_:) |
strikethroughColor(white:alpha:) |
||
strikethroughColor(hue:saturation:brightness:alpha:) |
||
strikethroughColor(red:green:blue:alpha:) |
||
strikethroughColor(patternImage:) |
||
NSUnderlineStyleAttributeName |
underlineStyle |
underlineStyle(_:) |
NSUnderlineColorAttributeName |
underlineColor |
underlineColor(_:) |
underlineColor(white:alpha:) |
||
underlineColor(hue:saturation:brightness:alpha:) |
||
underlineColor(red:green:blue:alpha:) |
||
underlineColor(patternImage:) |
||
NSStrokeWidthAttributeName |
strokeWidth |
strokeWidth(_:) |
NSStrokeColorAttributeName |
strokeColor |
strokeColor(_:) |
strokeColor(white:alpha:) |
||
strokeColor(hue:saturation:brightness:alpha:) |
||
strokeColor(red:green:blue:alpha:) |
||
strokeColor(patternImage:) |
||
NSShadowAttributeName |
shadow |
shadow(_:) |
shadow(color:offset:blurRadius:) |
||
NSTextEffectAttributeName |
textEffect |
textEffect(_:) |
NSAttachmentAttributeName |
attachment |
attachment(_:) |
NSLinkAttributeName |
link |
link(_:) |
link(string:) |
||
link(string:relativeToURL:) |
||
NSBaselineOffsetAttributeName |
baselineOffset |
baselineOffset(_:) |
NSObliquenessAttributeName |
obliqueness |
obliqueness(_:) |
NSExpansionAttributeName |
expansion |
expansion(_:) |
NSVerticalGlyphFormAttributeName |
verticalGlyphForm |
verticalGlyphForm(_:) |
段落样式
NSMutableParagraphStyle 属性 |
TextAttributes 属性 |
TextAttributes 方法 |
---|---|---|
对齐方式 |
对齐方式 |
对齐方式(_:) |
第一行首行缩进 |
第一行首行缩进 |
第一行首行缩进(_:) |
首行缩进 |
首行缩进 |
首行缩进(_:) |
尾部缩进 |
尾部缩进 |
尾部缩进(_:) |
行断行模式 |
行断行模式 |
行断行模式(_:) |
最大行高 |
最大行高 |
最大行高(_:) |
行间距 |
行间距 |
行间距(_:) |
段落间距 |
段落间距 |
段落间距(_:) |
段落前间距 |
段落前间距 |
段落前间距(_:) |
基文字方向 |
基文字方向 |
基文字方向(_:) |
行高乘数 |
行高乘数 |
行高乘数(_:) |
安装
Carthage
Carthage 是一个去中心化的依赖管理器,可以自动将框架添加到您的 Cocoa 应用程序中。
您可以使用以下命令使用 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 TextAttributes 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "delba/TextAttributes"
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。
您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 TextAttributes 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
use_frameworks!
pod 'TextAttributes'
许可
版权(c)2016-2019 Damien (http://delba.io)
在此特此授予任何获得本软件及其相关文档副本(“软件”)的人以无限制地处理该软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或将副本用于商业的目的,前提是遵守以下条件:
上述版权声明和许可声明应包含在软件的任何副本或实质部分的副本中。
本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任承担责任,无论基于合同行为、侵权或其他行为,这与软件或其使用或与其他软件或其使用有关或者与之相关。