(Objective-C) 自定义UITextView

自定义UITextView,支持placeholder、placeholderFont、placeholderColor

  • 使用方法
    self.textView = [[NBTextView alloc]initWithFrame:CGRectMake(0, 0, 400, 60)];
    [self.view addSubview:self.textView];
    self.textView.placeholder = @"请输入内容...";
    self.textView.placeholderColor = UIColor.lightGrayColor;
    self.textView.placeholderFont = [UIFont systemFontOfSize:20.0];
    self.textView.center = self.view.center;
    self.textView.backgroundColor = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.02];
  • 源码分享
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface NBTextView : UITextView

/// 占位符
@property (nonatomic, copy) NSString *placeholder;
/// 占位字体
@property (nonatomic, strong) UIFont *placeholderFont;
/// 占位字颜色
@property (nonatomic, strong) UIColor *placeholderColor;

@end

NS_ASSUME_NONNULL_END
#import "NBTextView.h"

@interface NBTextView()

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation NBTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    [self defaultConfig];
    [self setupPlaceholderLabel];
    [self addKeyboardNotifications];
    [self addObserverForTextChange];
    [self updatePlaceholderVisibility];
}

- (void)dealloc {
    [self removeKeyboardNotifications];
    [self removeObserverForTextChange];
}

- (void)defaultConfig {
    self.textContainerInset = UIEdgeInsetsZero;
    self.font = [UIFont systemFontOfSize:17.0];
    self.textContainer.lineFragmentPadding = 0;
}

- (void)setupPlaceholderLabel {
    if (!self.placeholderLabel) {
        self.placeholderLabel = UILabel.new;
        self.placeholderLabel.numberOfLines = 0;
        self.placeholderLabel.font =  self.font;
        self.placeholderLabel.textColor = UIColor.lightGrayColor;
        self.placeholderLabel.textAlignment = NSTextAlignmentNatural;
        self.placeholderLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self addSubview:self.placeholderLabel];
        [self layoutPlaceholderLabel];
    }
}

- (void)updatePlaceholderVisibility {
    self.placeholderLabel.hidden = (self.text.length > 0);
}

- (void)layoutPlaceholderLabel {
    UIEdgeInsets insets = self.textContainerInset;
    CGSize size = [self.placeholderLabel sizeThatFits:CGSizeMake(self.bounds.size.width - insets.left - insets.right,
                                                                 self.bounds.size.height - insets.top - insets.bottom)];
    CGPoint origin = CGPointMake(insets.left, insets.top);
    self.placeholderLabel.frame = CGRectMake(origin.x, origin.y, size.width, size.height);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutIfNeeded];
    [self layoutPlaceholderLabel];
}

- (void)addKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    self.placeholderLabel.hidden = YES;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [self updatePlaceholderVisibility];
}

- (void)addObserverForTextChange {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}

- (void)removeObserverForTextChange {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}

- (void)textDidChange:(NSNotification *)notification {
    [self updatePlaceholderVisibility];
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.placeholderLabel.text = placeholder;
    [self updatePlaceholderVisibility];
}

- (void)setPlaceholderFont:(UIFont *)placeholderFont {
    _placeholderFont = placeholderFont;
    self.placeholderLabel.font = placeholderFont;
    [self layoutPlaceholderLabel];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容