Build NSAttributedString using Attrs and style methods
masterAtributika provides a declarative API to build NSAttributedString from plain strings by identifying and styling specific patterns like HTML-like tags, hashtags, mentions, links, and phone numbers.
Basic Tag Styling
Use .style(tags:) to apply Attrs to specific HTML-like tags.
let b = Attrs().font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
label.attributedText = "Hello <b>World</b>!!!".style(tags: ["b": b]).attributedStringStyling Hashtags and Mentions
Use .styleHashtags() and .styleMentions() to automatically detect and style #hashtag and @mention patterns.
let str = "#Hello @World!!!"
.styleHashtags(Attrs().font(.boldSystemFont(ofSize: 45)))
.styleMentions(Attrs().foregroundColor(.red))
.attributedStringStyling Links and Phone Numbers
Use .styleLinks() for URLs and .stylePhoneNumbers() for telephone patterns.
let str = "Check this website http://google.com"
.styleLinks(Attrs().foregroundColor(.blue))
.attributedStringApplying a Base Style
Use .styleBase() to provide a default style for the entire string that isn't overridden by specific tag or pattern styling.
let base = Attrs().font(.systemFont(ofSize: 12)).foregroundColor(.gray)
let str = "Text with <b>bold</b>".styleBase(base).attributedStringlet b = Attrs().font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
label.attributedText = "Hello <b>World</b>!!!".style(tags: ["b": b]).attributedString