zoukankan      html  css  js  c++  java
  • 自定义textView,增加placeholder属性

    iOS文本输入框有两种:UITextField和UITextView。一般情况下,UITextField可以满足我们开发的需求,输入文字,系统自带placeHolder属性,直接用点语法赋值就可以实现功能需求。

    然而,有些时候我们可能会用到UITextView,系统提供的UITextView是没有自带placeHolder属性的。想要实现placeHolder样式有至少两种方法:

    1.添加一个label当作placeHolder的载体。实现UITextViewDelegate方法,监听文本输入框的输入状态,动态隐藏Label

    2.自定义UITextView,给UITextView添加一个placeHolder属性。简单方便地利用点语法直接赋值

    个人比较推崇第二种方法,其有点是:自定义一次,受用无穷,以后任何地方需要用到UITextView的placeHolder属性,直接创建就可以用,简单暴力。

    下面开始我们的自定义UITextView:(急用的可以直接粘代码)

    一、创建UITextView的子类,添加placeHolder属性(.h文件)

    @interface JQTextView : UITextView
    //placeHolder的文字
    @property(nonatomic,copy)NSString* placeHolderText;
    //placeHolder的颜色
    @property(nonatomic,strong)UIColor* placeHolderColor;
    
    @end

    二、在重写初始化方法的时候,创建出来placeHolder的载体label。所以需要先写出全局属性(.m文件)

    #import "JQTextView.h"
    @interface JQTextView()
    
    //可以改变隐藏状态的label
    @property(nonatomic,strong)UILabel* placeHolderLabel;
    
    @end

    三、重写初始化方法,同时创建出来placeHolder的载体label。在这里可能需要直接发送通知,监听textview的变化

    //重写init方法
    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            //创建label
            self.placeHolderLabel = [[UILabel alloc]init];
            //默认placeholder文字颜色
            self.placeHolderColor = [UIColor lightGrayColor];
            //可以换行
            self.placeHolderLabel.numberOfLines = 0;
            self.placeHolderLabel.backgroundColor = [UIColor clearColor];
            
            [self addSubview:self.placeHolderLabel];
            //发送通知,监听textview的变化
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textviewDidChange) name:UITextViewTextDidChangeNotification object:self];
        }
        return self;
    }
    
    #pragma mark - 监听textview变化
    -(void)textviewDidChange{
        //系统属性,hasText(是否有文字)
        self.placeHolderLabel.hidden = self.hasText;
    }

    注意点:有添加就要有销毁,在dealloc方法中销毁,千万不要忘记呀!!!

    四、重写layoutsubviews方法,配置label的frame

    //需要动态设置label的高度
    -(void)layoutSubviews{
        [super layoutSubviews];
        self.placeHolderLabel.mj_x = 5;
        self.placeHolderLabel.mj_y = 5;
        self.placeHolderLabel.mj_w = self.frame.size.width -10;
        //label高度判断
        CGSize maxSize = CGSizeMake(self.placeHolderLabel.mj_w, MAXFLOAT);
        
        /*
         NSString的对象方法,通过传入的参数返回一个CGRect数据,这个数据的size就是此时字符串显示成文本的尺寸
         options:
         1、NSStringDrawingUsesLineFragmentOrigin,整个文本将以每行组成的矩形为单位计算整个文本的尺寸
         2、NSStringDrawingUsesFontLeading,计算行高时使用行距
         3、NSStringDrawingUsesDeviceMetrics,计算布局时使用图元字形而不是印刷字体
         4、NSStringDrawingTruncatesLastVisibleLine,如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。
         */
        self.placeHolderLabel.mj_h = [self.placeHolderText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.placeHolderLabel.font} context:nil].size.height ;
        
    }

    五、重写setter方法

    #pragma mark - 重写setter方法
    -(void)setPlaceHolderText:(NSString *)placeHolderText{
        _placeHolderText = placeHolderText;
        
        self.placeHolderLabel.text = placeHolderText;
        
        [self setNeedsLayout];
    }
    -(void)setPlaceHolderColor:(UIColor *)placeHolderColor{
        _placeHolderColor = placeHolderColor;
        
        self.placeHolderLabel.textColor = placeHolderColor;
    }
    
    -(void)setFont:(UIFont *)font{
        [super setFont:font];
        self.placeHolderLabel.font = font;
        
        [self setNeedsLayout];
    }
    
    -(void)setText:(NSString *)text{
        [super setText:text];
        
        //调用通知方法
        [self textviewDidChange];
    }
    
    -(void)setAttributedText:(NSAttributedString *)attributedText{
        [super setAttributedText:attributedText];
        
        //调用通知方法
        [self textviewDidChange];
    }
    
    -(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:UITextViewTextDidChangeNotification];
    }

    以上,直接用我们自定义的TextView创建出来带有placeHolder属性的对象就可以了。点语法直接赋值:

    //商品描述
        self.productTextView = [[JQTextView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
        self.productTextView.placeHolderText = @"商品描述";
        
        self.productTextView.backgroundColor = [UIColor blackColor];
        
        self.productTextView.textColor = [UIColor whiteColor];
        [self.productView addSubview:self.productTextView];
  • 相关阅读:
    sujection重构
    serialize存入数组
    migration integer limit option
    FineReport——FS
    Oracle事务处理
    FineReport——发送邮件
    FineReport——登录不到决策系统
    FineReport——JS二次开发(复选框全选)
    FineReport——JS二次开发(局部刷新)
    FineReport——JS二次开发(工具栏按钮事件及说明)
  • 原文地址:https://www.cnblogs.com/yeschenbaby/p/5803643.html
Copyright © 2011-2022 走看看