测试测试过 | ✓ |
语言语言 | SwiftSwift |
授权协议 | MIT |
发布日期最后发布 | 2017年10月 |
SwiftSwift 版本 | 3.2 |
SPM 支持 SPM | ✗ |
由 Junmo Kim 维护。
JKAttributedString
提供了 NSAttributedString
的 Swift API。
在 Swift 的值类型世界中使用 NSAttributedString
和 NSMutableAttributedString
疲倦了吗?
这使得处理属性字符串和 String
一样简单。
var aString = JKAttributedString(string: "Whatever",
attributes: [.color(.gray), .font(.systemFont(ofSize: 12))])
aString.adding(attributes: [.color(.red)]) // Red "Whatever"
let bString = "Want".attributed([.font(.systemFont(ofSize: 20))]) // Size 20
// bString.adding( ... ) <- COMPILE ERROR
let cString = aString + " You " + bString
// Whatever You { color: red, size: 12 }Want{ size: 20 }
let dString = "Whenever You " + bString // Size 20
let testString = "Doing 테스트 ".attributed([.color(.gray)])
+ "🎯 です。¶".attributed([.color(.blue)])
testString.attributes(at: testString.index(testString.startIndex, offsetBy: 9)) // Gray
testString.attributes(at: testString.index(testString.startIndex, offsetBy: 10)) // Blue
enum SystemFontAttribute: JKAttributeType {
case normal(CGFloat)
case bold(CGFloat)
var name: String {
return NSFontAttributeName
}
var value: Any {
switch self {
case .normal(let size):
return UIFont.systemFont(ofSize: size)
case .bold(let size):
return UIFont.boldSystemFont(ofSize: size)
}
}
}
let systemString = "System".attributed([SystemFontAttribute.bold(20)])