I'm a beginner in Swift and I want to make a custom Label on some letters the font to be bigger, and on some others to be smaller.
Like on image:
So, the currency symbol and decimal points should be in a smaller font than other numbers.
The code I tried to implement is:
// Used this function to get the index of decimal point (.) from the string.
func getDecimalIndex(text: String) -> Int {
let myText = text.firstIndex(of: ".")
return myText?.utf16Offset(in: text) ?? 0
}
func setupLabel(text: String) {
guard !text.isEmpty else { return }
let attributedString = NSMutableAttributedString(string: text)
let decimalStart = getDecimalIndex(text: attributedString.string)
attributedString.addAttribute(.font,
value: UIFont.systemFont(ofSize: 22),
range: NSRange(location: decimalStart, length: attributedString.length))
myLabel.attributedText = attributedString
}
setupLabel(text: "$1100.33")
setupLabel(text: "1100.33€")
Whenever I load the app, it crashes on this line:
attributedString.addAttribute(.font,
value: UIFont.systemFont(ofSize: 22),
range: NSRange(location: decimalStart, length: attributedString.length))
And the error in console is:
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
What might be wrong?
Thank you in advance for your contribution.
Instead of setting
NSRangeforattributedStringyou can use severalstringwith severalattributedString-attributes.use this function by splitting your string with
.