Created
October 27, 2021 18:31
-
-
Save christianselig/0c183cbaea7051c6bb7c5db1e13d8b1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let items = [ | |
"Cats walking down to the mall to hang out at the water fountain", | |
"Dogs catching tennis balls by the hotel pool", | |
"Fish doing backflips as they hone their circus aspirations" | |
] | |
let mainAttributedString = NSMutableAttributedString() | |
for (index, item) in items.enumerated() { | |
// Get the highest number however you want, this is hardcoded for sake of example | |
let highestNumber = 577 | |
// Ternary hell, don't judge | |
let numberPrefix = index == 0 ? 1 : index == 1 ? 22 : highestNumber | |
let font = UIFont.systemFont(ofSize: 15.0) | |
let tabWidth: CGFloat = 15.0 | |
// If you don't ceil this it won't format properly, probably internal integer rounding | |
let prefixWidth = ceil(NSAttributedString(string: "\(highestNumber).", attributes: [.font: font]).size().width) | |
let itemParagraphStyle = NSMutableParagraphStyle() | |
itemParagraphStyle.tabStops = [ | |
NSTextTab(textAlignment: .right, location: prefixWidth), | |
NSTextTab(textAlignment: .left, location: prefixWidth + tabWidth) | |
] | |
itemParagraphStyle.headIndent = prefixWidth + tabWidth | |
let itemAttributedString = NSAttributedString(string: "\t\(numberPrefix).\t\(item)\n", attributes: [.font: font, .foregroundColor: UIColor.black, .paragraphStyle: itemParagraphStyle]) | |
mainAttributedString.append(itemAttributedString) | |
} | |
label.attributedText = mainAttributedString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment