教大家怎样给UITextView加入placeholder扩展

怎样扩展UITextView以追加placeholder功能呢?

我们的需求是:追加placeholder功能

方案讨论:

  • 通过继承UITextView的方式
  • 通过扩展UITextView的方式

分析:方案1使用继承方式实现起来更简单,可是使用起来就没有那么方便;方案2 使用扩展的方式,实现起来稍比前者复杂。可是外部使用起来更简单

方案定位:採用扩展的方式,以极简的风格作为參考依据。

Tip:所谓极简。即对外接口最简,对内部能够非常复杂

扩展头文件

#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
##说明:头文件里事实上仅仅须要公开hdf_placeholderLabel属性就能够实现了,可是依旧公开了对hdf_placeholderLabel直接属性,能够依据个人习惯使用。扩展是不能扩展属性的。可是这里写成了属性的形式。事实上仅仅是利用了getter/setter方式。重写其API。

内部会使用动态执行时机制来完毕扩展伪属性的功能(称为伪属性是由于本质上是扩展不了属性的)


实现文件

#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
说明:这里的实现文件里使用了执行时机制(runtime)来实现,这里对文本改变的监听,交给了自己和UIApplication。是为了兼容到IOS6.0,在6.0下。交给自己是不可行的,会崩溃,因此移交给UIApplication单例对象来管理。

############提示:这里使用了Masonary这个自己主动布局的三方库。让这个placeholderlabel自己主动依据uitextview的大小变化而变化。代码中使用了推断IOS系统。推断是否为空串的代码,这里不写出来了,自己替掉就可以。

以下就是使用了,使用起来就非常easy了

  self.remarkView.text = @"哈哈,非常easy吧";
  self.remarkView.hdf_placeholder = @"写点什么...";

备注:当需求中须要给textview加入placeholder时,网上的都是继承的方式。还须要改动非常多地方,这是不可行的。所以我才提出这个问题来,然后做出了自己的解决方式。假设对大家实用,请放心地拿去用吧。

兼容IOS6.0及其以上版本号
posted @ 2017-07-15 14:56  zsychanpin  阅读(384)  评论(0编辑  收藏  举报