0

同じものを比較してもうまくいかないのはなぜ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];
}
4

2 に答える 2

0

textView のデフォルト フォントが Helvetica/system - サイズ 12 に設定されているかどうかを確認します。これは、属性付き文字列の最後の部分が、テキストを設定した後に textView のデフォルト フォントを取得していることが原因である可能性があります。しかし、aString (uvwxyz) の一部にはフォントが割り当てられていないため、一致しません。

これがお役に立てば幸いです:)

于 2014-08-20T09:20:00.453 に答える
0

そのテキストビューを強く型付けされた変数にして試してください。例えば。

@property(nonatomic, strong)UITextView * aTextView;
于 2014-08-20T09:14:18.827 に答える