第一版本(并不能完美的適配8.0iOS手機(jī))
一開始采用kvc的方法,主要使用[self setValue:label forKey:@"_placeholderLabel"]
對(duì)UITextView的私有屬性進(jìn)行賦值。
// UITextView+Extension.m
// UITextVIew-placeholder
#import "UITextView+Extension.h"
#import <objc/runtime.h>
@implementation UITextView (Extension)
+ (void)load {
// 獲取類方法 class_getClassMethod
// 獲取對(duì)象方法 class_getInstanceMethod
Method setFontMethod = class_getInstanceMethod(self, @selector(setFont:));
Method was_setFontMethod = class_getInstanceMethod(self, @selector(was_setFont:));
// 交換方法的實(shí)現(xiàn)
method_exchangeImplementations(setFontMethod, was_setFontMethod);
}
- (void)was_setPlaceholderWithText:(NSString *)text Color:(UIColor *)color{
//防止重復(fù)設(shè)置 cell復(fù)用等問(wèn)題
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[view removeFromSuperview];
}
}
//設(shè)置占位label
UILabel *label = [[UILabel alloc] init];
label.text = text;
label.font = self.font;
label.textColor = color;
[self addSubview:label];
[self setValue:label forKey:@"_placeholderLabel"];
//第一次會(huì)有位移
self.text = @"1";
self.text = @"";
}
- (void)was_setFont:(UIFont *)font{
//調(diào)用原方法 setFont:
[self was_setFont:font];
//設(shè)置占位字符串的font
UILabel *label = [self valueForKey:@"_placeholderLabel"];
label.font = font;
NSLog(@"%s", __func__);
}
@end
之后在bugly上發(fā)現(xiàn)報(bào)錯(cuò):
設(shè)備機(jī)型
iPhone 6 Plus
系統(tǒng)版本
8.1.3 (12B466)
崩潰內(nèi)容:
[<UITextView 0x12d165200> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key _placeholderLabel.
LilyForParent -[UITextView(Extension) was_setPlaceholderWithText:Color:] (UITextView+Extension.m:)
bugly顯示有錯(cuò)誤日志,并不是iOS8.0以上系統(tǒng)都支持,不能找到
_placeholderLabel
這個(gè)關(guān)鍵字,所以感覺(jué)KVC不能使用,也不知道是不是其他的原因。
改進(jìn)型
思路
通過(guò)runtime關(guān)聯(lián)對(duì)象方法,關(guān)聯(lián)一個(gè)私有Label變量,實(shí)現(xiàn)占位字符串的功能。
使用方法交換將TextView的font與placeholderlabel的font綁定,同時(shí)變化大小。
//
// UITextView+Placeholder.m
#import "UITextView+Placeholder.h"
#import <objc/runtime.h>
static const char *kPlaceholderKey = "kPlaceholderKey";
static const char *kPlaceholderLabelKey = "kPlaceholderLabelKey";
@interface UITextView (PlaceholderLabel)<UITextViewDelegate>
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation UITextView (Placeholder)
+ (void)load {
// 獲取類方法 class_getClassMethod
// 獲取對(duì)象方法 class_getInstanceMethod
Method setFontMethod = class_getInstanceMethod(self, @selector(setFont:));
Method was_setFontMethod = class_getInstanceMethod(self, @selector(was_setFont:));
// 交換方法的實(shí)現(xiàn)
method_exchangeImplementations(setFontMethod, was_setFontMethod);
}
- (void)was_setFont:(UIFont *)font{
//調(diào)用原方法 setFont:
[self was_setFont:font];
//設(shè)置占位字符串的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是實(shí)驗(yàn)后得出的完美位置,如有需求可以修改
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拖入項(xiàng)目中,在需要使用TextView的控制器內(nèi)導(dǎo)入,調(diào)用下面方法可以
- (void)was_setPlaceholderWithText:(NSString *)placeholder Color:(UIColor *)color;
- 如果有好的建議可以直接留言或者發(fā)送郵件:andysection@gmail.com
Thank you for your reading !
Github:Demo