Make Ints within a string bold - swift

Hi I have a string that I'm displaying in a label that shows a time like this "4hours 27mins 45secs" and this can vary based on the time for example "30mins 5secs"

I want to style the Ints in the string so they are bold and the characters are light so it looks like this...

screenshot

I've done some searching and found I need to use some sort of attributed string, but everything I've found seems to use some sort of breaking character, or a standard prefix to detect.

As I don't have either is there a way to check if the character is an int and apply bold. Appreciate this might not be possible and would welcome a recommendation for changing my approach.

Still pretty new to coding/swift so any help appreciated!


Quickly done, you can use a Regular Expression to find the ranges of all the numbers. Then, on these ranges, you change the attribute for a bold font.

let attributedString = NSMutableAttributedString(string: str, attributes: [.foregroundColor: UIColor.white])
let regex = try! NSRegularExpression(pattern: "\\d+", options: [])
let matches = regex.matches(in: attributedString.string, options: [], range: NSRange(location: 0, length: attributedString.length))
matches.forEach {
    attributedString.addAttributes([.font: UIFont.boldSystemFont(ofSize: 17)], range: $0.range)
}