示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行 pod install
AttributedTextView
插入单个标签
let textView = AttributedTextView()
let model = TextModel(id: "id-0", text:"hashtag", symbolStr: "#", image: nil, type: 0)
textView.insertSpecialText(model: model, isDeleteLastString: false)
解析标签字符串
textView.modelMapper = { text -> TextModel? in
let splits = text.split()
guard
splits.count >= 4,
let id = splits[safe: 1]?.description,
let type = splits[safe: 2]?.description.int,
let text = splits[safe: 3]?.description
else { return nil }
switch type {
case 0: return TextModel(id: id, text: text, symbolStr: "#", image: nil, type: type)
case 1: return TextModel(id: id, text: text, symbolStr: "@", image: nil, type: type)
default: return TextModel(id: id, text: text, symbolStr: nil, image: #imageLiteral(resourceName: "location"), type: type)
}
}
textView.set(text: text)
AttributedLabel
let label = AttributedLabel()
label.modelMapper = { text -> TextModel? in
let splits = text.split()
guard
splits.count >= 4,
let id = splits[safe: 1]?.description,
let type = splits[safe: 2]?.description.int,
let text = splits[safe: 3]?.description
else { return nil }
switch type {
case 0: return TextModel(id: id, text: text, symbolStr: "#", image: nil, type: type)
case 1: return TextModel(id: id, text: text, symbolStr: "@", image: nil, type: type)
default: return TextModel(id: id, text: text, symbolStr: nil, image: #imageLiteral(resourceName: "location"), type: type)
}
}
label.clickLink = { model in
guard let m = model else { return }
debugPrint(m.text)
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
let defaultAttributes: [NSAttributedString.Key : Any] = [
.font: UIFont.systemFont(ofSize: 15),
.foregroundColor: UIColor.black,
.paragraphStyle: paragraphStyle,
]
let attr = NSAttributedString(string: text, attributes: defaultAttributes)
label.set(attributedString: attr)
安装
AttributedText 通过CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'AttributedText'
作者
许可证
AttributedText 在 MIT 许可证下提供。更多信息请参阅 LICENSE 文件。