CascadeKit
CascadeKit 是一个库,可以根据内容语言(确切地说,是内容脚本)来应用样式到 UIKit 文本控件。
您可以在我们的 公司 Medium 博客 上了解更多关于这个库的信息。
构建状态
分支 | 状态 |
---|---|
主分支 |
目录
如何使用 CascadeKit
CascadeKit 允许您根据文本内容轻松应用 UI 定制。假设您想将蓝色应用到拉丁字符,红色应用到俄语字符
let mutAttrString = NSMutableAttributedString(string: text, attributes: originalAttributes)
let newAttr = mutAttrString.addAttributes(for: [.latin, .russian, .russianSupplementary], including: [.whiteSpace]) { (fallback) -> [Attribute] in
switch fallback.type {
case .latin:
return [
Attribute(key: .foregroundColor, value: UIColor.blue, range: fallback.range)]
case .russian, .russianSupplementary:
return [Attribute(key: .foregroundColor, value: red, range: fallback.range)]
default:
return []
}
}
textView.attributedText = newAttr
很简单,对吧?
我们可以增强这个例子,对 Attributed String 进行更多自定义,如下所示:
- 希腊语
- 前景颜色 = 蓝色
- 字体 = Helvetica Neue, 15
- 俄语
- 背景颜色 = 红色
- 前景颜色 = 白色
- 字体 = Courier, 18
let mutAttrString = NSMutableAttributedString(string: text, attributes: originalAttributes)
let newAttr = mutAttrString.addAttributes(for: [.greek, .greekExtended, .russian, .russianSupplementary], including: [.whiteSpace]) { (fallback) -> [Attribute] in
switch fallback.type {
case .greek, .greekExtended:
return [
Attribute(key: .foregroundColor, value: UIColor.blue, range: fallback.range),
Attribute(key: .font, value: UIFont(name: "Helvetica Neue", size: 15), range: fallback.range)]
case .russian, .russianSupplementary:
return [
Attribute(key: .backgroundColor, value: UIColor.red, range: fallback.range),
Attribute(key: .foregroundColor, value: UIColor.white, range: fallback.range),
Attribute(key: .font, value: UIFont(name: "Courier", size: 18), range: fallback.range)]
default:
return []
}
}
textView.attributedText = newAttr
设置自定义规则的步骤如下:
- 定义 UI 需要关注的字母列表,即
let myAlphabets = [.greek, .greekExtended, .russian, .russianSupplementary]
- 在
addAttributes
回调中实现您的自定义规则
您精心制作的内容准备好在世界上的每种语言中显示!
如何安装 CascadeKit
CocoaPods
将 CascadeKit 添加到您的 Podfile
use_frameworks!
target 'MyTarget' do
pod 'CascadeKit', '~> 1.2.3'
end
$ pod install
将 CascadeKit 源代码添加到您的项目
将 CascadeKit 文件夹 添加到您的项目中。
如何贡献
贡献非常受欢迎
对于任何信息或请求,请随时打开一个 问题。
许可和致谢
许可
CascadeKit 可在 MIT 许可证下使用。更多信息请参阅 LICENSE 文件。