Reputation: 7778
I am trying to make a dynamic UILabel like this:
"I Love Watching The BBC"
As the label is dynamic, I will have no idea of its contents. I can leave the text alone and let the user define what they want. I can capitalize the whole string, the first letter of each word, or all words in a string.
The problem is that when capitalizing, words that are entered as uppercase become lower case.
So, in the example above
BBC
becomes
Bbc
I've searched all over the web, and don't think there is a way to do this.
As requested, code so far:
cell.projectName?.text = projectNameArray[indexPath.row].localizedCapitalized
Upvotes: 1
Views: 832
Reputation: 180
I suggest you to add this extension to your code.
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).capitalized + dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
(Form Hacking With Swift)
Also for the UILabel.text
property apply a custom function that split each word in string, and apply the capital letter.
An example could be that:
extension String {
func capitalizeWords() -> String {
self.split(separator: " ")
.map({ String($0).capitalizingFirstLetter() })
.joined(separator: " ")
}
}
The complexity could be decreased I think, but it's just a working hint ;)
Upvotes: 1