UItextView Placeholderの追加

4631 ワード

第1版(8.0 iOS携帯電話に完璧に対応できない)
最初はkvc法を用い,主に[self setValue:label forKey:@"_placeholderLabel"]を用いてUItextViewのプライベート属性を付与した.
//  UITextView+Extension.m
//  UITextVIew-placeholder

#import "UITextView+Extension.h"
#import 

@implementation UITextView (Extension)

+ (void)load {
    
    //       class_getClassMethod
    //        class_getInstanceMethod
    
    Method setFontMethod = class_getInstanceMethod(self, @selector(setFont:));
    Method was_setFontMethod = class_getInstanceMethod(self, @selector(was_setFont:));
    
    //        
    method_exchangeImplementations(setFontMethod, was_setFontMethod);
}

- (void)was_setPlaceholderWithText:(NSString *)text Color:(UIColor *)color{
    //       cell     
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
    }

    //    label
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = self.font;
    label.textColor = color;
    
    [self addSubview:label];
    [self setValue:label forKey:@"_placeholderLabel"];
    
    //       
    self.text = @"1";
    self.text = @"";
}

- (void)was_setFont:(UIFont *)font{
    //      setFont:
    [self was_setFont:font];
    //        font
    UILabel *label = [self valueForKey:@"_placeholderLabel"];
    label.font = font;
    NSLog(@"%s", __func__);
}
@end


その後buglyでエラーが見つかりました.
デバイス機種iPhone 6 Plus
システムバージョン8.1.3(12 B 466)
クラッシュ内容:[setValue:forUndefinedKey:]:this class is not key value coding-compliant for the key_placeholderLabel.LilyForParent-[UItextView(Extension)was_setPlaceholderWithText:Color:](UItextView+Extension.m:)buglyにエラーログが表示され、iOS 8.0以上のシステムでサポートされているわけではなく、_placeholderLabelというキーワードが見つからないので、KVCが使えないような気がしますし、他の理由が分からないような気がします.
改良型
構想
runtime関連オブジェクトメソッドにより、プライベートLabel変数を関連付け、占有文字列の機能を実現します.
メソッド交換を使用してTextViewのfontをplaceholderlabelのfontにバインドし、サイズを変更します.
//
//  UITextView+Placeholder.m

#import "UITextView+Placeholder.h"
#import 

static const char *kPlaceholderKey = "kPlaceholderKey";
static const char *kPlaceholderLabelKey = "kPlaceholderLabelKey";

@interface UITextView (PlaceholderLabel)

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation UITextView (Placeholder)

+ (void)load {
    
    //       class_getClassMethod
    //        class_getInstanceMethod
    
    Method setFontMethod = class_getInstanceMethod(self, @selector(setFont:));
    Method was_setFontMethod = class_getInstanceMethod(self, @selector(was_setFont:));
    
    //        
    method_exchangeImplementations(setFontMethod, was_setFontMethod);
}

- (void)was_setFont:(UIFont *)font{
    //      setFont:
    [self was_setFont:font];
    //        font
    if (self.placeholderLabel != nil) {
        self.placeholderLabel.font = font;
        NSLog(@"%s", __func__);
    }
}

- (UILabel *)placeholderLabel{
    return objc_getAssociatedObject(self, kPlaceholderLabelKey);
}

- (void)setPlaceholderLabel:(UILabel *)placeholderLabel{
    objc_setAssociatedObject(self, kPlaceholderLabelKey, placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)placeholder{
    return objc_getAssociatedObject(self, kPlaceholderKey);
}

- (void)was_setPlaceholderWithText:(NSString *)placeholder Color:(UIColor *)color{
    objc_setAssociatedObject(self, kPlaceholderKey, placeholder, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    UILabel *label = [[UILabel alloc] init];
    label.text = placeholder;
    label.textColor = color;
    label.numberOfLines = 0;
    [label sizeToFit];
    [self addSubview:label];
    
    self.placeholderLabel = label;
    
    self.delegate = self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //5  7.75           ,        
    self.placeholderLabel.frame = CGRectMake(5, 7.75, self.frame.size.width, self.frame.size.height);
    [self.placeholderLabel sizeToFit];
}

- (void)textViewDidChange:(UITextView *)textView{
    self.placeholderLabel.hidden = !(self.text.length == 0);
}

@end

使用方法
プロジェクトにUItextView+Placeholder.hとUItextView+Placeholder.mを直接ドラッグ&ドロップし、TextViewを使用する必要があるコントローラにインポートし、次の方法を呼び出します.
- (void)was_setPlaceholderWithText:(NSString *)placeholder Color:(UIColor *)color;
  • 良いアドバイスがあれば、直接メッセージを送ったり、メールを送ったりすることができます[email protected]

  • Thank you for your reading !
    Github:Demo