I am having a problem updating the text on a UILabel
. My ViewController
changes its view property phoneNumberString
. My view observes this property change and reacts, changing the label's attributed text. It observes using ReactiveCocoa.
[[RACAble(self.phoneNumberString) distinctUntilChanged]subscribeNext:^(id x) {
NSString* s=(NSString*)x;
UIFont* font=[UIFont systemFontOfSize:numbersFontSize weight:UIFontWeightThin];
NSMutableDictionary* attributesDict=[NSMutableDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil];
self.phoneNumberLabel.attributedText = [[NSAttributedString alloc]initWithString:s attributes:attributesDict];
[self.phoneNumberLabel setNeedsDisplay];
NSLog(@"label att text = %@\nlabel text = %@\ninput string = %@\n==",self.phoneNumberLabel.attributedText,self.phoneNumberLabel.text,x);
}];
It provides the following log:
label att text = +7 (225{
NSFont = "<UICTFont: 0x7a2942c0> font-family: \".SFUIDisplay-Thin\"; font-weight: normal; font-style: normal; font-size: 25.00pt";
}
label text = +7 (225
input string = +7 (225
I need attributed text to be located in my UILabel
. The problem is that it provides no changes in the simulator. (Using native [self observeValueForKeyPath...]
results in the same effect).
I've solved this problem using notifications with the same code. But I have to use KVO.
Additional information:
Here is my initialization code (before setting the observer):
UILabel* phoneNumberLabel=[[UILabel alloc]init]; font=[UIFont systemFontOfSize:numbersFontSize weight:UIFontWeightThin]; attributesDict=[NSMutableDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]; phoneNumberLabel.attributedText=[[NSAttributedString alloc]initWithString:@"+7 " attributes:attributesDict];
Both methods run on the main thread.
- The
UILabel
's frame is big enough to allocate.