TextFormater
一个将 String 转换为 NSAttributedString 的 Pod。
用于将包含格式化命令的纯文本字符串转换为格式化字符串。主要用于 Label、Button 和小型文本框。大型页面可以直接使用 NSAttributedString.init(html:documentAttributes:)
来实现。
将包含格式化命令的字符串转换为 NSAttributedString。这主要用于 Label、Button 和小型文本框。对于大型页面,建议使用 NSAttributedString.init(html:documentAttributes:)
。
感谢
新版本基于 MarkdownKit 和 iosMath 实现
新 2.0 版本是基于 MarkdownKit 和 iosMath 开发的
示例
可以运行示例应用来测试不同格式命令的效果。
示例项目可以用于测试格式命令。
要运行示例项目,首先克隆仓库,然后从示例目录运行 pod install
。
- iOS 演示应用
- OSX 演示应用
需求
- iOS 11.0
- OSX 10.13
安装
TextFormater 可以通过CocoaPods安装:
TextFormater 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
注意 iosMath 需要最新版,可使用github版本
注意 新版 iosMath 需要
pod 'iosMath', :git => 'https://github.com/kostub/iosMath.git'
pod "TextFormater"
用法 - 用法
- 实现图片数据获取代理
- 遵守 GetImageForTextFormater 协议
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()
)
imageDelegate
- 必须提供图片获取代理
- 必须提供图片代理(用于 img 命令)
命令 - 格式命令
命令嵌套 - nest
不支持同一命令嵌套使用
不支持嵌套相同命令
<br>
换行。该命令不需要结束标签 (</>
)。
换行。此命令不需要闭合标签。
<img>key</img>
插入图片。
插入图片。
<center>.*</center>
居中。自动在首尾添加两个换行符(\n
)
居中对齐。自动在首尾添加换行符。
<color black|blue|red|green|yellow|...>.*</color>
设置前景色。
设置前景色。
<bgcolor black|blue|red|green|yellow|...>.*</bgcolor>
设置背景色。
设置背景色。
$方程式$
行内模式下的数学公式。文本模式下的 LaTeX 方程式。
$$方程式$$
单行居中数学公式。显示模式下的 LaTeX 方程式。
作者
许可
TextFormater 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。