比较2个相同的NSAttributedStrings不起作用

时间:2014-08-20 08:36:17

标签: ios ios7 nsstring nsattributedstring nsmutableattributedstring

为什么比较相同的NSAttributedString不起作用?

我有以下内容:

- (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];
}

2 个答案:

答案 0 :(得分:0)

使该textview强类型变量并尝试。例如。

@property(nonatomic, strong)UITextView * aTextView;

答案 1 :(得分:0)

检查你的textView是否有默认字体设置为Helvetica / system - size 12.可能是因为你的属性字符串的最后一部分是在setText之后采用textView的默认字体。但是aString(uvwxyz)的一部分没有分配字体,因此它不匹配。

希望这可以帮助你:)