- December 30, 2024
- Mins Read
This library is a Swift port/fork of the popular Objective-C library TTTAttributedLabel. Much ❤️ and credit goes to Mattt for creating such a great UILabel replacement library.
Nantes
is a pure-Swift UILabel
replacement. It supports attributes, data detectors, and more. It also supports link embedding automatically and with NSTextCheckingTypes
.
Come build awesome things with us here at Instacart!
Nantes is available through Carthage. To install it, add the following line to your Cartfile
:
github “instacart/nantes”
Nantes is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod ‘Nantes’
To run the example project, clone the repo, and run pod install
from the Example directory first.
Check out Nantes
in the Example
directory for more examples.
import Nantes
let label: NantesLabel = .init(frame: .zero)
label.attributedTruncationToken = NSAttributedString(string: “… more”)
label.numberOfLines = 3
label.labelTappedBlock = {
label.numberOfLines = label.numberOfLines == 0 ? 3 : 0 // Flip between limiting lines and not
UIView.animateWithDuration(0.2, animations: {
self.view.layoutIfNeeded()
})
}
label.text = “Nantes label is great! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ac urna et ante lobortis varius. Nunc rhoncus enim vitae sem commodo sodales. Morbi id augue id augue finibus tincidunt. Cras ac massa nisi. Maecenas elementum vitae elit eu mattis. Duis pretium turpis ut justo accumsan molestie. Mauris elit elit, maximus eu risus sed, vestibulum sodales enim. Sed porttitor vestibulum tincidunt. Maecenas mollis tortor quam, sed porta justo rhoncus id. Phasellus vitae augue tempor, luctus metus sit amet, dictum urna. Morbi sit amet feugiat purus. Proin vitae finibus lectus, eu gravida erat.”
view.addSubview(label)
let linkLabel: NantesLabel = .init(frame: .zero)
linkLabel.delegate = self // NantesLabelDelegate
linkLabel.linkAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
linkLabel.text = “https://www.instacart.com”
view.addSubview(linkLabel)
// Link handling
func attributedLabel(_ label: NantesLabel, didSelectLink link: URL) {
print(“Tapped link: \(link)”)
}