🚨V5 版本现已发布!🚨

Atributika 是一个 Swift 库,它提供了一种简单的方法,可以通过识别和样式化标签、链接、电话号码、话题标签等,从类似 HTML 的文本构建 NSAttributedString。

一个独立的 AtributikaViews 库提供了 UILabel/UITextView 的直接替换,能够显示可高亮和可点击的链接,具有丰富的自定义功能和适当的辅助功能支持。

注意

试试我用于自动布局的新库,它是 Visual Format Language 的类型安全重新构想: https://github.com/psharanda/FixFlex

介绍

虽然 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

AtributikaViews

V5

V5 是该项目在 2023 年初进行的一次重大重写。它与之前的版本不完全兼容,需要进行一些手动迁移。为了项目的进一步发展,引入破坏性更改是必要的。

以下是新增功能

NSAttributedString 构建

AttributedLabel / AttributedTextView

新的示例已添加到演示应用程序中,包括

示例

检测和样式化标签,为字符串的其余部分提供基本样式,不要忘记特殊的 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>&lt;a&gt;</a>tributik<a>&lt;/a&gt;</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

超级字符串

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)

要求

当前版本兼容

注意: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"

手动

  1. 使用 git submodule add https://github.com/psharanda/Atributika.git 将 Atributika 添加到你的项目中作为子模块
  2. 打开 Atributika 文件夹 & 将 Atributika.xcodeproj 拖到你的项目树中
  3. Atributika.framework 添加到你的目标的 Link Binary with Libraries 构建阶段
  4. 使用 import Atributika 导入 Atributika,你就可以开始了

许可证

Atributika 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。