测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可 | MIT |
发布最新发布 | 2016年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Ashik uddin Ahmad 维护。
AKAttributeKit 是一种新的有趣的方法来创建 NSAttributedString,它实现了同样的功能,但不需要生成。使用 HTML 样式的标签来设置属性而不是查找范围。
// ---- Native way to NSAttributedString ----//
var mStr = NSMutableAttributedString(string: "Hello Attributed String!")
var range = (mStr.string as NSString).rangeOfString("Hello")
mStr.addAttributes([
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont(name: "Arial", size: 25)!
], range: range)
// ---- AKAttributeKit way to NSAttributedString ----//
mStr = "<fg #f00><font Arial|25>Hello</font></fg> Attributed string!".toAttributedString()
// or
mStr = AKAttributeKit.parseString("<fg #f00><font Arial|25>Hello</font></fg> Attributed string!")
查看 示例项目 和/或 playground 以查看和尝试 AKAttributeKit。
要运行示例项目,首先从仓库中克隆,然后从示例目录运行 pod install
。
有一些库可以从 HTML 创建 NSAttributedString,但这并不是那样的。AKAttributeKit 专注于原生 API,并提供了实现这些 API 的直接方法。AKAttribute 标签和 HTML 标签之间的基本区别如下:
| (竖线)
-分隔的序列值提供。标签 | 属性 | 示例 |
---|---|---|
a | NSLinkAttributeName | <a http://google.com>Google</a> |
base | NSBaselineOffsetAttributeName | square<base 15>2</base> |
bg | NSBackgroundColorAttributeName | <bg #00ff00>Green</bg> 或 <bg 255|255|0>Yellow</bg> |
ex | NSExpansionAttributeName | <ex 5>WIDE</ex> |
fg | NSForegroundColorAttributeName | <fg #ff0000>Red</fg> 或 <fg 0|0|255>Blue</fg> |
font | NSFontAttributeName | Different <font Arial|18>Font</font> |
i | NSObliquenessAttributeName | <i 0.5>Italic</i> 或 <i 0.8>oblique</i> |
k | NSKernAttributeName | <k 20>Huge Space</k> |
sc ,sw | NSStrokeColorAttributeName, NSStrokeWidthAttributeName | <sc #f00><sw 2>Storked Text</sw></sc> |
t ,tc | NSStrikethroughStyleAttributeName, NSStrikethroughColorAttributeName | <t 1>Wrong</t> 或 <t><tc #f00>Wrong</t></tc> |
u ,uc | NSUnderlineStylePropertyName, NSUnderlineColorPropertyName | <u>重要</u> 或 <u 1><uc #f00>重要</u></uc> |
注意:通常不需要
tc
或uc
。但是当使用时,需要在范围内使用t
或u
分别应用。同样,sc
和sw
需要配合使用,以表明可见属性的改变。
以下是参数类型的简短列表以及可接受的格式
类型 | 可接受的格式 | 应用标签 |
---|---|---|
链接 | 任何有效的URL格式 | a |
Int | 相应属性支持的任何整数值 | t , u |
Float | 任何浮点值 | base , ex , i , k , sw |
颜色 | 1. 十六进制格式: rgb , rgba , rrggbb , rrggbbaa 带或不带 0x 或 # 前缀2. 整数序列: r|g|b|a 参数序列,其中序列中的所有参数都是从0到255的整数范围。3. 插入 UIColor: 直接在 swift 字符串中插入 UIColor,例如 <tag \(myColor)> ,其中 myColor 是除了 colorWithPatternImage 之外任何 UIColor。 | bg , fg , sc , tc , uc |
字体 | 1. 参数序列: fontName|fontSize 参数序列,其中 fontName 是字符串,fontSize 是浮点数2. 插入 UIFont: 直接在 swift 字符串中插入 UIFont,例如 <font \(myFont.asAKAttribute())> | font |
AKAttributeKit 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod "AKAttributeKit"
以下是计划要实现的一些待办事项。对新想法或对当前任务的帮助都表示欢迎 :D
Ashik uddin Ahmad, [email protected]
AKAttributeKit 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。