0

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:

  1. 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];
    
  2. Both methods run on the main thread.

  3. The UILabel's frame is big enough to allocate.
Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
Dmitriy
  • 81
  • 2
  • 10
  • http://stackoverflow.com/questions/27728466/use-multiple-font-colors-in-a-single-label-swift/27728516#27728516 – Kirit Modi Apr 22 '16 at 13:15
  • Have you tried using `RACObserve` instead of `RACAble`? I've had much greater success with that one. Also, where are you setting up the RAC binding, viewDidLoad? – fullofsquirrels Apr 22 '16 at 17:56
  • Yes, i've tried. I've also tried with native observing. I set it up in initialization of the view, wich contains label – Dmitriy Apr 24 '16 at 13:35
  • You might try setting up the RAC bindings in `-viewDidLoad` instead; it's possible that not all of the objects are fully instantiated at the point that you're trying to set up the binding. Also, looking again at what you've posted, I don't see where `self.phoneNumberString` is actually being set; can you post that additional code as well? – fullofsquirrels Apr 25 '16 at 17:57

3 Answers3

3

If you have used size classes in .xib to set font of label then attributed text may not work. To work it properly remove size classes from .xib file

Martin
  • 846
  • 1
  • 9
  • 23
1

Try calling your method in viewDidLayoutSubviews. For me it did the trick. I was doing it in viewDidLoad and it was an issue.

serhii.syrotynin
  • 241
  • 1
  • 14
0

It looks like you are missing an assignment of the attribute to a range of the string. For example,

NSMutableAttributedString *attributedPhoneNumber = [[NSMutableAttributedString alloc] initWithString:x];
[attributedPhoneNumber addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attributedPhoneNumber length])];
self.phoneNumberLabel.attributedText = attributedPhoneNumber;
user212514
  • 3,110
  • 1
  • 15
  • 11
  • This post: http://stackoverflow.com/questions/13029222/nsattributedstring-default-color-font-etc seems to indicate that, initially, if you don't specify a range, then `initWithString:attributes:` would use a default range of the whole string, which confirms my experience with it. However, I haven't been able to find anything in Apple's docs to corroborate. – fullofsquirrels Apr 22 '16 at 20:13
  • I didn't know that. I went based on code that I had used successfully. Thanks for the info! – user212514 Apr 22 '16 at 22:25