The designer of the app I'm currently working on suddenly wanted to capitalise the text in every table view cell in the app.
In (nearly) every tableview, I'm using a custom UITableViewCell
called 'NormalListCell'. NormalListCell has a UILabel
property called 'mainLabel', which is the one i want capitalised.
One way to do it would be to do this everywhere:
//Previously: cell.mailLabel.text = _someLowerCaseString;
cell.mailLabel.text = [_someLowerCaseString uppercaseStringWithLocale:[NSLocale currentLocale]]];
But that seems like the worst possible way to do it since I'll have to manually go through every file that uses NormalListCell. Also, there's no guarantee that the design won't change again.
KVO seemed like a decent idea.
- (void)awakeFromNib {
[super awakeFromNib];
[self.mainLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.mainLabel) {
[self.mainLabel setText:[change[@"new"] uppercaseStringWithLocale:[NSLocale currentLocale]]];
}
}
But this will not work since I'm observing the 'text' property and setting it in -observeValueForKeyPath: would call this method again.
I could stop observing, set the text of the label, and add self as the observer again but it seems like a pretty hacky solution.
Is this the 'correct' solution? Is there a better way to do it?
One better approach would be creating a custom label and adding it on your custom cell.
UILabel
setText
methodNormalListCell