ios UITextView设置站位文字

1、很多需求中有需要多行输入,需要站位文字的输入框,这时候最好使用UITextView,下面就新建一个类,继承自UITextView

#import <UIKit/UIKit.h>

@interface PlaceholderTextView : UITextView

/** 占位符*/
@property (copy ,nonatomic) NSString *placeholder;
/** 占位符颜色*/
@property (strong ,nonatomic) UIColor *placeholderColor;
/** 占位符字号*/
@property (strong ,nonatomic) UIFont *placeholderFont;
/** 占位符尺寸*/
@property (assign ,nonatomic) CGRect placeholderRect;


@end
#import "PlaceholderTextView.h"

@interface PlaceholderTextView()<UITextViewDelegate>

@end

@implementation PlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
//        self.font = [UIFont systemFontOfSize:15];
        self.placeholderColor = [UIColor grayColor];
        // 监听文字改变
        [QFMNotiCenter addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}


/**
 *绘制站位文字 (每次调用此方法之前,会自动清除掉之前的内容)
 */
- (void)drawRect:(CGRect)rect {
    
    // 如果有文字,直接返回,不绘制站位文字
//    if (self.text.length || self.attributedText.length) return;
    if (self.hasText) return;

    // 处理rect
    rect.origin.x = 3;
    rect.origin.y = 7;
    rect.size.width -= 2*rect.origin.x;
    
    // 文字属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.placeholderFont;
    attrs[NSForegroundColorAttributeName] = self.placeholderColor;
//    [self.placeholder drawInRect:CGRectMake(3, 7, self.width, self.height) withAttributes:attrs];
    [self.placeholder drawInRect:rect withAttributes:attrs];
    
}

- (void)textDidChange:(UITextView *)textView{
    QFMLOGFUNC;
    [self setNeedsDisplay];
}


- (void)dealloc{
    [QFMNotiCenter removeObserver:self];
}


#pragma mark - **************** 重写setter 防止外部改写属性
- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

- (void)setPlaceholderFont:(UIFont *)placeholderFont{
    _placeholderFont = placeholderFont;
    [self setNeedsDisplay];
}
- (void)setText:(NSString *)text{
    [super setText:text];
    [self setNeedsDisplay];
}

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

2、第二种方法

.m文件

#import "PlaceholderTextView.h"

@interface PlaceholderTextView()<UITextViewDelegate>

/** 站位文字label*/
@property(weak ,nonatomic)UILabel *placeholderLabel;

@end

@implementation PlaceholderTextView

#pragma mark - **************** 第二种方法

-(UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        // 添加一个用来显示站位文字的label
        UILabel *placeholderLabel = [[UILabel alloc] init];
//        placeholderLabel.hidden = YES;
        placeholderLabel.x = 4;
        placeholderLabel.y = 7;
        placeholderLabel.numberOfLines = 0;
        [self addSubview:placeholderLabel];
        self.placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 竖直方向永远可以拖动
        self.alwaysBounceVertical = YES;
        self.font = [UIFont systemFontOfSize:15];
        // 默认的站位文字颜色
        self.placeholderColor = [UIColor grayColor];
        [QFMNotiCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

/*
 *更新站位文字尺寸
* */
- (void)updatePlaceholderLabelSize{
    CGSize placeholderSize = CGSizeMake(self.width - 2*self.placeholderLabel.x, MAXFLOAT);
    self.placeholderLabel.size = [self.placeholder boundingRectWithSize:placeholderSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;

//self.placeholderLabel.width = SCREEN_WIDTH - 2*self.placeholderLabel.x;
  //  [self.placeholderLabel sizeToFit]; 此方法亦可以

}

#pragma mark - **************** 重新setter
-(void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}

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

- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self updatePlaceholderLabelSize];
}

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

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


- (void)textDidChange{

    self.placeholderLabel.hidden = self.hasText;
}
- (void)dealloc{
    [QFMNotiCenter removeObserver:self];
}


@end

3、第三种方法

#pragma mark - **************** 第三种方法

-(UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        // 添加一个用来显示站位文字的label
        UILabel *placeholderLabel = [[UILabel alloc] init];
        //        placeholderLabel.hidden = YES;
        placeholderLabel.x = 4;
        placeholderLabel.y = 7;
        placeholderLabel.numberOfLines = 0;
        [self addSubview:placeholderLabel];
        self.placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 竖直方向永远可以拖动
        self.alwaysBounceVertical = YES;
        self.font = [UIFont systemFontOfSize:15];
        // 默认的站位文字颜色
        self.placeholderColor = [UIColor grayColor];
        [QFMNotiCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    // 根据textView的宽度计算placeholder的宽度
    self.placeholderLabel.width = self.width - 2*self.placeholderLabel.x;
    [self.placeholderLabel sizeToFit];
    
}

#pragma mark - **************** 重新setter
-(void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}

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

- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self setNeedsLayout];
}

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

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


- (void)textDidChange{

    self.placeholderLabel.hidden = self.hasText;
}
- (void)dealloc{
    [QFMNotiCenter removeObserver:self];
}


/**
 * setNeedsDisplay方法:会在恰当的时刻自动调用drawRect方法
 * setNeedsLayout方法:会在恰当的时刻调用layoutSubViews方法
 */
@end

此方法更为严谨

用label设置的一个好处是,站位文字可以拖动

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 前段时间,身边的朋友聊起他最近听的小众的民谣,他说自己听不懂,一是没有明白那种音乐的呈现方式,二是没有听明白歌词或...
    惊蛰小白阅读 593评论 0 1
  • 搜索引擎营销是我普遍认为可以让用户主动找上门的一种营销方式。如,我们经常在用百度搜索一些需要解决的问题,一些需要了...
    飞马哥阅读 546评论 0 2
  • 作业本一:思维空性 衣服的空性:我喜欢淑女款式的衣服,有的人喜欢流行款式的衣服,都是对的,衣服是具备空性的,当有观...
    妙莲修行阅读 143评论 0 0
  • 任凭别人调侃我单身是因为没有人喜欢我,无所谓,我自己也会开玩笑说因为自己太差了,不会有人喜欢,或者很郑重其事的说...
    柯家妍阅读 741评论 0 6