TextFormater

一个将字符串转换为 NSAttributedString 的 pod

CI Status Version License Platform

用来将包含格式化命令的纯文本字符串转换为格式字符串。主要设计用于 Label、Button 和小型文本框。大型页面可以直接使用 NSAttributedString.init(html:documentAttributes:)来实现。

将包含格式化命令的字符串转换为 NSAttributedString。主要设计用于 Label、Button 和小型文本框。 对于大型页面,NSAttributedString.init(html:documentAttributes:) 可能是更好的选择。

感谢

新版实现基于MarkdownKitiosMath实现

新 2.0 版本基于 MarkdownKitiosMath 构建。

示例

可以运行样例 app 来测试不同格式命令的效果。

要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 `pod install`。

Example app

Example OSX app

要求

安装

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()
        )

imageDelegate

命令 - 格式命令

命令嵌套 - nest

不支持同一命令嵌套使用

不支持嵌套相同的命令


<br>

换行。该命令不需要结束 (</>)。

换行。此命令不需要闭合 (</>)。

<img>key</img>

插入图片。

插入图片。

<center>.*</center>

居中。自动在首尾增加两个换行(\n)

居中对齐。在头部和尾部添加换行符 (\n)。

<color black|blue|red|green|yellow|...>.*</color>

设置前景色。

设置前景色。

<bgcolor black|blue|red|green|yellow|...>.*</bgcolor>

设置背景色。

设置背景色。

$equation$

行内模式数学公式。 文本模式下的 LaTeX 公式。

$$equation$$

单行居中数学公式。 显示模式下的 LaTeX 公式。

作者

Alfred Gao, alfredg@alfredg.cn

许可

TextFormater 基于 MIT 许可。 有关更多信息,请参见 LICENSE 文件。