#import <UIKit/UIKit.h>
/**
* @author huangyibiao
*
* 给UITextView添加placeholder
*
* @note 注意,此扩展有点小问题,如果在添加placeholder后,又直接赋值Text属性,则不会自动消失
* 解决办法是:如果有初始值,先给text,再设置holder
*/
@interface UITextView (HDFTextView)
/**
* 占位提示语
*/
@property (nonatomic, copy) NSString *hdf_placeholder;
/**
* 占位提示语的字体颜色
*/
@property (nonatomic, strong) UIColor *hdf_placeholderColor;
/**
* 占位提示语的字体
*/
@property (nonatomic, strong) UIFont *hdf_placeholderFont;
/**
* 占位提示语标签
*/
@property (nonatomic, strong, readonly) UILabel *hdf_placeholderLabel;
@end
#import "UITextView+HDFTextView.h"
static const void *s_hdfTextViewPlaceholderLabelKey = "s_hdfTextViewPlaceholderLabelKey";
static const void *s_hdfTextViewPlaceholderTextKey = "s_hdfTextViewPlaceholderTextKey";
@interface UIApplication (HDFTextViewHolder)
@end
@implementation UIApplication (HDFTextViewHolder)
- (void)hdf_placehoderTextChange:(NSNotification *)nofitication {
if (kIsIOS7OrLater) {
return;
}
UITextView *textView = nofitication.object;
if ([textView isKindOfClass:[UITextView class]]) {
if (!kIsEmptyString(textView.text)) {
textView.hdf_placeholderLabel.text = @"";
} else {
textView.hdf_placeholderLabel.text = textView.hdf_placeholder;
}
}
}
@end
@interface UITextView (HDFPlaceholderTextView)
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation UITextView (HDFPlaceholderTextView)
- (void)setPlaceholderLabel:(UILabel *)placeholderLabel {
objc_setAssociatedObject(self,
s_hdfTextViewPlaceholderLabelKey,
placeholderLabel,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)placeholderLabel {
UILabel *label = objc_getAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey);
if (label == nil || ![label isKindOfClass:[UILabel class]]) {
label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.font = self.font;
label.backgroundColor = [UIColor clearColor];
label.textColor = kHolderTipColor;
[self addSubview:label];
kWeakObject(self);
self.placeholderLabel = label;
CGFloat left = kIsIOS7OrLater ? 5 : 7;
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(weakObject).insets(UIEdgeInsetsMake(7.5, left, 0, 0));
}];
label.enabled = NO;
[kNotificationCenter addObserver:kIsIOS7OrLater ? self : [UIApplication sharedApplication]
selector:@selector(hdf_placehoderTextChange:)
name:UITextViewTextDidChangeNotification
object:nil];
}
return label;
}
@end
@implementation UITextView (HDFTextView)
- (void)hdf_placehoderTextChange:(NSNotification *)notification {
if (kIsIOS7OrLater) {
if (!kIsEmptyString(self.text)) {
self.placeholderLabel.text = @"";
} else {
self.placeholderLabel.text = self.hdf_placeholder;
}
}
}
- (UILabel *)hdf_placeholderLabel {
return self.placeholderLabel;
}
- (void)setHdf_placeholder:(NSString *)hdf_placeholder {
if (kIsEmptyString(hdf_placeholder)) {
objc_setAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self.placeholderLabel removeFromSuperview];
return;
}
objc_setAssociatedObject(self,
s_hdfTextViewPlaceholderTextKey,
hdf_placeholder,
OBJC_ASSOCIATION_COPY_NONATOMIC);
if (!kIsEmptyString(self.text)) {
self.placeholderLabel.text = @"";
} else {
self.placeholderLabel.text = hdf_placeholder;
}
}
- (NSString *)hdf_placeholder {
return objc_getAssociatedObject(self, s_hdfTextViewPlaceholderTextKey);
}
- (void)setHdf_placeholderColor:(UIColor *)hdf_placeholderColor {
self.placeholderLabel.textColor = hdf_placeholderColor;
}
- (UIColor *)hdf_placeholderColor {
return self.placeholderLabel.textColor;
}
- (void)setHdf_placeholderFont:(UIFont *)hdf_placeholderFont {
self.placeholderLabel.font = hdf_placeholderFont;
}
- (UIFont *)hdf_placeholderFont {
return self.placeholderLabel.font;
}
@end
self.remarkView.text = @"哈哈,很简单吧";
self.remarkView.hdf_placeholder = @"写点什么...";
版权声明:本文为博主原创文章,未经博主允许不得转载。
教大家如何给UITextView添加placeholder扩展
原文地址:http://blog.csdn.net/woaifen3344/article/details/47313901