美文网首页控件类
ios UITextView设置站位文字

ios UITextView设置站位文字

作者: fulen | 来源:发表于2017-04-05 09:34 被阅读39次

    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 UITextView设置站位文字

        本文链接:https://www.haomeiwen.com/subject/kndtattx.html