attributedText
UILabel と UITextView のプロパティを操作するメソッドを定義する UIView カテゴリがあります。
@implementation UIView (replaceAttrText)
-(void) replaceAttrText: (NSString *)str {
if ([self respondsToSelector: @selector(setAttributedText)]) {
NSMutableAttributedString *labelText = [self template];
// change it
[self performSelector:@selector(setAttributedText) withObject: labelText];
}
}
@end
RespondsToSelector は、UILabel と UITextView の両方に対して false を返し (ただし、setAttributedText に応答します)、setAttributedText がrespondsToSelector
チェックなしで直接実行されると、例外が発生します。
カテゴリが UILabel に (セレクターなしで) 直接実装されている場合、すべてが機能しますが、残念ながら UILabel と UITextView には attributedText プロパティを持つ共通の祖先がありません。
私は何を間違っていますか?ありがとう!