Tribulus provides very convenient way to compose attributed strings.
It is a framework based on applying custom Attributes
class and nice chaining syntax.
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew with the following command:
$ brew update
$ brew install carthage
To integrate Tribulus into your Xcode project using Carthage, specify it in your Cartfile
:
github "DmitryFrishbuter/Tribulus"
Run carthage update
to build the framework and drag the built Tribulus.framework
into your Xcode project.
Drag Sources
folder from latest release into your project.
To initialize attributed string with required attributes, you can use following code:
let attributedString = NSAttributedString(string: "Foo", style:
TextStyle.font(.systemFont(ofSize: 14))
.color(.black)
.backgroundColor(.cyan)
.baselineOffset(14.0)
)
Tribulus also allows appending to existing mutable attributed string.
Here's a code for appending new attributed string:
let mAttributedString = NSAttributedString(string: "Foo")
.asMutable()
.append(" Bar", with: .color(.red))
.append(" Baz", with: .direction(.horizontal))
And in the same simple way you can append any image:
let attributedString = NSMutableAttributedString(string: "Foo")
attributedString.append(UIImage(named: "Bar")!, bounds: CGRect(x: 0, y: 0, width: 40, height: 40))
Moreover Tribulus allows you to insert newly configured attributed string at any location:
let attributedString = NSMutableAttributedString(string: "Foo Baz")
attributedString.insert("Bar", with: .textEffect(.letterpressStyle), at: 4)
or to insert an image:
attributedString.insert(UIImage(named: "Bar")!,
bounds: CGRect(x: 0, y: 0, width: 40, height: 40),
at: 0)
Instead on creating UIFontDescriptorSymbolicTraits
you can just set bold and italic traits using Attributes
object properties:
let attributedString = NSMutableAttributedString(string: testString)
attributedString.set(TextStyle.bold(true).italic(true))
Dmitry Frishbuter, [email protected]
Tribulus is available under the MIT license. See the LICENSE file for more info.