扩展UITextView占位符和清空控能

本文介绍了如何自定义一个YKTextView类,以实现类似UITextField的占位符功能和清除按钮。通过设置placehoderLabel和clearButton,实现了在文本内容变化时自动显示或隐藏占位符和清除按钮。同时,还提供了设置首行缩进的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 定义YKTextViewMode
typedef NS_ENUM(NSInteger, YKTextViewMode) {
    YKTextViewModeNever,
    YKTextViewModeWhileEditing,
};

#import <UIKit/UIKit.h>

@interface YKTextView : UITextView
@property (nonatomic, strong) NSString *placehoder; // 占位文字
@property (nonatomic, strong) UIColor *placehoderColor; // 占位文字颜色
@property (nonatomic, assign) BOOL showPlacehoder; // 输入后是否显示占位文字
@property (nonatomic, assign) YKTextViewMode clearButtonMode;

@end



#import "YKTextView.h"
@interface YKTextView ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *placehoderLabel;
@property (nonatomic, strong) UIButton *clearButton;
@end
@implementation YKTextView

- (UIButton *)clearButton {
    if (!_clearButton) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_clearButton setBackgroundImage:[UIImage imageNamed:@"clearImage"] forState:UIControlStateNormal];
        _clearButton.backgroundColor = [UIColor clearColor];
        _clearButton.hidden = YES;
        [_clearButton addTarget:self action:@selector(clearAllText) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        UILabel *placehoderLabel = [[UILabel alloc] init];
        placehoderLabel.numberOfLines = 0;
        placehoderLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:placehoderLabel];
        self.placehoderLabel = placehoderLabel;
        self.placehoderColor = [UIColor lightGrayColor];
        self.font = kFont(17);
        self.placehoderLabel.font = kFont(17);
        self.clearButton.frame = CGRectMake(self.size.width - 17.5, self.size.height - 17.5, 15, 15);
        [self addSubview:self.clearButton];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)clearAllText {
    
    self.text = @"";
}

- (void)setClearButtonMode:(YKTextViewMode)clearButtonMode {
    _clearButtonMode = clearButtonMode;
}


- (void)textDidChange {
    if (self.clearButtonMode == YKTextViewModeNever) {
        self.clearButton.hidden = YES;
    } else {
        self.clearButton.hidden = NO;
    }
    if (!self.showPlacehoder) {
        self.placehoderLabel.hidden = YES;
    } else {
        self.placehoderLabel.hidden = NO;
    }
    


}
- (void)textViewDidChange:(UITextView *)textView {
    

}

- (void)setText:(NSString *)text {
    [super setText:text];
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    [self textDidChange];
}

- (void)setPlacehoder:(NSString *)placehoder {
    _placehoder = placehoder;
    self.placehoderLabel.text = placehoder;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.placehoderLabel.mj_x = 15,
    self.placehoderLabel.mj_y = 10;
    self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.mj_x;
    CGFloat height = [self.placehoder boundingSize:CGSizeMake(self.placehoderLabel.width, MAXFLOAT) attributes:@{NSFontAttributeName : kFont(17), NSForegroundColorAttributeName : self.placehoderColor}];
    self.placehoderLabel.height = height;
    
}

// 此方法在为重写textViewDidChange进行首行缩进

#pragma mark - 设置首行缩进
-(void)textViewDidChange:(UITextView *)textView{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 4;    //行间距
    paragraphStyle.maximumLineHeight = 60;   /**最大行高*/
    CGFloat width = [self.myTextView.placehoder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFont(16), NSForegroundColorAttributeName : RGBColor(51, 51, 51)} context:nil].size.width;
    paragraphStyle.firstLineHeadIndent = width + 10;    /**首行缩进宽度*/
    paragraphStyle.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:16],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值