Atributika
是一个Swift库,它提供了一种简单的方式来从类似HTML的文本中构建NSAttributedString,通过识别和样式标记、链接、电话号码、标签等。
独立的AtributikaViews
库提供UILabel/UITextView的简单替换,能够显示可高亮和可点击的链接,具有丰富的自定义功能和合适的辅助功能支持。
简介
虽然NSAttributedString无疑非常强大,但它也是一个低级API,需要进行相当多的设置工作。如果您的字符串是一个模板,而实际内容只有在运行时才知道,那么这变得很复杂。处理本地化时,构建NSAttributedString也不太直接。
但等等,Atributika
来救你了!
let b = Attrs().font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
label.attributedText = "Hello <b>World</b>!!!".style(tags: ["b": b]).attributedString
当然,这要简单得多。Atributika
易于使用,声明式、灵活,并且为您处理了粗糙的边缘。
特性
Atributika
- NSAttributedString构建器。
- 使用自定义的高速解析器检测和样式类似HTML的标签。
- 检测和样式标签和提及(即,#something和@someone)。
- 识别和样式链接和电话号码。
- 检测并格式化正则表达式和NSDataDetector模式。
- 格式化整个字符串或指定的部分。
- 允许将上述功能链起来,以解析复杂的字符串!
- 提供干净且具有表达力的API来构建样式。
- 提供一组独立的检测工具,供独立使用。
- 兼容iOS、tvOS、watchOS和macOS。
Atributika视图
- 带有可突出显示和可点击链接的自定义视图。
- 为正常/突出显示/禁用状态提供自定义文本样式。
- 支持自定义高亮。
V5
V5是在2023年初对项目的重大重写。它与之前的版本不完全兼容,需要进行一些手动迁移。引入破坏性更改对于项目的进一步发展势在必行。
以下是新特性
NSAttributedString构建
- 完全重写的HTML解析器,修正了许多错误并改进了对边缘情况的处理。
- 更通用、更健壮的文本转换和属性微调API。
AttributedLabel / AttributedTextView
- 迁移到独立库,与Atributika独立。
- 提供适当的可访问性支持。
- 改进性能和触摸处理。
- AttributedLabel基于UILabel(轻量级,文本垂直居中)。
- AttributedTextView基于UITextView(支持滚动和文本选择,文本对齐到帧顶部)。
在演示应用程序中添加了新的示例,包括
- 由AttributedTextView驱动的基本网络浏览器
- SwiftUI集成
- Markdown文档的突出显示链接
示例
检测和格式化标签,为字符串的其余部分提供基本样式,不要忘记特殊HTML符号
let redColor = UIColor(red:(0xD0 / 255.0), green: (0x02 / 255.0), blue:(0x1B / 255.0), alpha:1.0)
let a = Attrs().foregroundColor(redColor)
let font = UIFont(name: "AvenirNext-Regular", size: 24)!
let grayColor = UIColor(white: 0x66 / 255.0, alpha: 1)
let base = Attrs().font(font).foregroundColor(grayColor)
let str = "<a><a></a>tributik<a></a></a>"
.style(tags: ["a": a])
.styleBase(base)
.attributedString
检测并样式化哈希标签和提及
let str = "#Hello @World!!!"
.styleHashtags(Attrs().font(.boldSystemFont(ofSize: 45)))
.styleMentions(Attrs().foregroundColor(.red))
.attributedString
检测并样式化链接
let str = "Check this website http://google.com"
.styleLinks(Attrs().foregroundColor(.blue))
.attributedString
检测并样式化电话号码
let str = "Call me (888)555-5512"
.stylePhoneNumbers(Attrs().foregroundColor(.red))
.attributedString
Uber字符串
let links = Attrs().foregroundColor(.blue)
let phoneNumbers = Attrs().backgroundColor(.yellow)
let mentions = Attrs().font(.italicSystemFont(ofSize: 12)).foregroundColor(.black)
let b = Attrs().font(.boldSystemFont(ofSize: 12))
let u = Attrs().underlineStyle(.single)
let base = Attrs().font(.systemFont(ofSize: 12)).foregroundColor(.gray)
let str = "@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika"
.style(tags: ["u": u, "b": b])
.styleMentions(mentions)
.styleHashtags(links)
.styleLinks(links)
.stylePhoneNumbers(phoneNumbers)
.styleBase(base)
.attributedString
AttributedLabel(属于标签)
let tweetLabel = AttributedLabel()
tweetLabel.numberOfLines = 0
tweetLabel.highlightedLinkAttributes = Attrs().foregroundColor(.red).attributes
let baseLinkAttrs = Attrs().foregroundColor(.blue)
let a = TagTuner {
Attrs(baseLinkAttrs).akaLink($0.tag.attributes["href"] ?? "")
}
let hashtag = DetectionTuner {
Attrs(baseLinkAttrs).akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of: "#", with: ""))")
}
let mention = DetectionTuner {
Attrs(baseLinkAttrs).akaLink("https://twitter.com/\($0.text.replacingOccurrences(of: "@", with: ""))")
}
let link = DetectionTuner {
Attrs(baseLinkAttrs).akaLink($0.text)
}
let tweet = "@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika"
tweetLabel.attributedText = tweet
.style(tags: ["a": a])
.styleHashtags(hashtag)
.styleMentions(mention)
.styleLinks(link)
.attributedString
tweetLabel.onLinkTouchUpInside = { _, val in
if let linkStr = val as? String {
if let url = URL(string: linkStr) {
UIApplication.shared.openURL(url)
}
}
}
view.addSubview(tweetLabel)
需求
当前版本兼容以下
- Swift 5.0+
- iOS 11.0 或更高版本
- tvOS 11.0 或更高版本
- watchOS 4.0 或更高版本
- macOS 10.13 或更高版本
注意:AttributedLabel
/ AttributedTextView
只在 iOS 平台上可用
为什么 Atributika 的名字中有一个小写的 't'?
因为在白俄罗斯语/俄语中,我们有一个相同的字母 't'(атрибутыка/атрибутика)。所以基本上,它是转录,而不是一个真实存在的单词。
集成
Swift Package Manager
将依赖项添加到 Package.swift
文件中。
dependencies: [
.package(url: "https://github.com/psharanda/Atributika.git", .upToNextMajor(from: "5.0.0"))
]
Carthage
将 github "psharanda/Atributika"
添加到您的 Cartfile
CocoaPods
Atributika 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod "Atributika"
pod "AtributikaViews"
手动操作
- 使用
git submodule add https://github.com/psharanda/Atributika.git
将 Atributika 添加到您的项目中作为子模块 - 打开 Atributika 文件夹,将
Atributika.xcodeproj
拖到您的项目树中 - 将
Atributika.framework
添加到目标的Link Binary with Libraries
构建阶段 - 使用
import Atributika
导入 Atributika,您就可以开始使用了
许可证
Atributika 下的 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。