0

Why does comparing the same NSAttributedString not work?

I have the following:

- (void)someSetupClass {

    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:@"abcdefghijklmnopqrstuvwxyz"];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
    [aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];

    _aTextView.attributedText = aString;
    _aTextView.delegate = self;

    [_scroll addSubview:_best];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {

    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:@"abcdefghijklmnopqrstuvwxyz"];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
    [aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];

    if ([textView.attributedText isEqualToAttributedString:aString]) {

        // Never reaches here, but why?

        textView.text = @"";

        // textView.textColor = [UIColor blackColor];
    }

    [textView becomeFirstResponder];
}
jbouaziz
  • 1,484
  • 1
  • 12
  • 24
cdub
  • 24,555
  • 57
  • 174
  • 303
  • typo but still not working let me edit – cdub Aug 20 '14 at 08:39
  • If you log `textView.attributedText` and `aString` is the output the same? The thing is that you seem to be playing both with `attributedText` and `text` properties of your `UITextView`. Could be the issue (one is `nil`)? – Larme Aug 20 '14 at 10:55

2 Answers2

0

Make that textview strongly typed variable and try. Eg.

@property(nonatomic, strong)UITextView * aTextView;
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
0

Check if your textView has default font set to Helvetica/system - size 12. May be because of this last part of your attributed string is taking default font of the textView after you setText. But part of aString (uvwxyz) does not have font assigned and hence it mismatches.

Hope this helps you :)

Pradnya
  • 36
  • 2