一个将字符串转换为 NSAttributedString 的 pod
用来将包含格式化命令的纯文本字符串转换为格式字符串。主要设计用于 Label、Button 和小型文本框。大型页面可以直接使用 NSAttributedString.init(html:documentAttributes:)
来实现。
将包含格式化命令的字符串转换为 NSAttributedString。主要设计用于 Label、Button 和小型文本框。 对于大型页面,NSAttributedString.init(html:documentAttributes:)
可能是更好的选择。
新版实现基于MarkdownKit和iosMath实现
新 2.0 版本基于 MarkdownKit 和 iosMath 构建。
可以运行样例 app 来测试不同格式命令的效果。
要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install
。
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 `pod install`。
TextFormater 可通过CocoaPods安装:
TextFormater 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
注意 iosMath 需要最新版,可使用github版本
注意 需要最新版本的 iosMath
pod 'iosMath', :git => 'https://github.com/kostub/iosMath.git'
pod "TextFormater"
func getImage(byKey: String) -> UIImage? {
switch byKey {
case "50":
return #imageLiteral(resourceName: "img50")
default:
return nil
}
let textFormater = TextFormater()
textFormater.imageDelegate = self
textResult.attributedText = textFormater.parse(textCode.text)
在创建对象时可设置字体
建议在 init() 期间自定义字体
public init(fontFamilies:[String] = ["Verdana","苹方-简"],
fontSize:CGFloat = 0,
color:MarkdownColor = MarkdownParser.defaultColor,
boldFontFamilies:[String] = ["Didot","Hei"],
boldFontSize:CGFloat = 0,
boldFontColor:MarkdownColor = MarkdownParser.defaultColor,
italicFontFamilies:[String] = ["Times New Roman","Kai"],
italicFontSize:CGFloat = 0,
italicFontColor:MarkdownColor = MarkdownParser.defaultColor,
equationFontSize:CGFloat = 0,
equationColor:MarkdownColor = MarkdownParser.defaultColor,
imageDelegate:GetImageForTextFormater = NilImageDelegate()
)
不支持同一命令嵌套使用
不支持嵌套相同的命令
换行。该命令不需要结束 (</>
)。
换行。此命令不需要闭合 (</>
)。
插入图片。
插入图片。
居中。自动在首尾增加两个换行(\n
)
居中对齐。在头部和尾部添加换行符 (\n
)。
设置前景色。
设置前景色。
设置背景色。
设置背景色。
行内模式数学公式。 文本模式下的 LaTeX 公式。
单行居中数学公式。 显示模式下的 LaTeX 公式。
Alfred Gao, alfredg@alfredg.cn
TextFormater 基于 MIT 许可。 有关更多信息,请参见 LICENSE 文件。