ZenText 是管理您 iOS 应用文本的最简单方式。使用 ZenText,您可以
ZenText 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "ZenText"
要运行示例项目,请克隆代码库,并首先从 Example 目录运行 pod install
您会注意到复制可以包含参数。您只需使用 $0, $1, … 来设置您的参数。在文本中的顺序并不重要。
// english chat copy. prefix is for establishing context (optional)
ZenText.manager.config.addCopy("en", prefix: "chat") {
return [
"dance": ["@$0 dances with themself"], // note: this is chat.dance
]
}
// spanish chat copy
ZenText.manager.config.addCopy("sp", prefix: "chat") {
return [
"dance": ["@$0 baila con sí mismos"], // note: this is chat.dance
]
}
NSAttributedString
myLabel.attributedText = ZenText.manager.attributedString(key: "global.fun", args: [sender, other])
ZenText.manager.config.setStyles {
return [
"action": Style(
color: .lightGrayColor()
),
"token": Style(
color: .blueColor(),
fontSize: 14
),
"hulk": Style (
color: .greenColor(),
fontSize: 40
)
]
}
let atUserRegex = "(@[A-Za-z0-9_]*)"
mutableAttributedString.regexFind(atUserRegex, addStyle: "token")
在这个例子中,chat.poke 有 $0 和 $1 作为“token”样式的参数
ZenText.manager.config.addCopy("en", prefix: "chat") {
return [
...
"poke": ["@$0 ".style("token"), "pokes ", "@$1".style("token")] // note: this is chat.poke
]
}
我建议在您项目中的一个名为 Copy.swift 的文件(您可以将它命名为任何您喜欢的名字)中存储所有字符串。我还建议使用复制前缀来根据上下文分离您的键(例如,“global”,“profile”,“chat”等)
如果您有任何问题,请告诉我!请提交反馈、问题和拉取请求 :)
JP McGlone, [email protected]
ZenText 在 MIT 许可证下可用。更多请参阅 LICENSE 文件。