自定义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];
时间: 2025-01-01 21:07:09

自定义textView,增加placeholder属性的相关文章

自定义textView的placeholder和边框

想实现的效果: // //  ViewController.m //  A14 - 带占位符的textview // //  Created by vic fan on 16/6/24. //  Copyright © 2016年 李洪强. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @property(nonatomi

HTML5增加的属性

---恢复内容开始--- 表单属性   1.为input(text).select.textarea与button元素新增加autofocus属性. 它以指定属性的方式让元素在画面打开时自动获得焦点. 2.为input元素与textarea元素新增加placeholder属性,它会对用户的输入进行提示, 提示用户可输入的内容. 3.为input.output.select.textarea.button与fieldset新增加form属性,声明它属于哪个表单, 然后将其放置在页面上任何位置,而不

Android开发学习笔记-自定义TextView属性模版

如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多. 下面是自定义样式模版的方法. 1.在style.xml文件中添加自己要设置的样式内容 <resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devic

自定义UITextView的placeholder(占位文字)

我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入.2.UITextField有placeholder属性,而UITextView没有.至于两者的代理方法,原理基本差不多,只是方法名略有差异. 如何为UITextView添加一个placeholder功能呢,其实方法很简单,三步即可实现: 1.在创建textView的时候

placeholder属性

placeholder属性 是在input输入框内提示内容(内容自定义)如下图所示: position: absolute:绝对定位并且不占位置: relative:相对定位占位置 我们在设置定位时一般是采用 子绝父相 的原则,但是也可以用 子绝父绝 的模式,但是这个时候父级元素就不占位置,脱离标准流...

自定义textView限制字数

ViewController.m #import "JYZTextView.h" #define kTextBorderColor     RGBCOLOR(227,224,216)   #undef  RGBCOLOR #define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] @interface ViewController ()<UITextViewDe

Android开发之自定义UI组件和属性

Android系统虽然自带了很多的组件,但肯定满足我们个性化的需求,所以我们为了开发方便,需要自定义Android的UI组件,以实现我们个性化的需求. 自定义组合控件的步骤: 1 .自定一个View,需要继承相对布局,线性布局等ViewGroup的子类.ViewGroup是一个其他控件的容器,能够乘放各种组件. 2 .实现父类的3个构造方法.一般需要在构造方法里始化初自定义布局文件. 一个参数构造方法:为new控件使用 两个参数的造方法:在调用布局文件使用 两个参数的造方法:传递带有样式的布局文

翻翻git之---不靠画全靠“演”,好看的自定义TextView translucent-android

转载请注明出处:王亟亟的大牛之路 P1:废话部分 上周五又没上班所以没怎么写东西,礼拜5 入了条地图,昨天开食了,有时间再去搞两条别的然后再买个缸(家里的缸,鱼啊别的动物已经好多了..次哦)0.0 不然他们大了,别的鱼要倒霉了...哈哈 白红的那位 麦麦: 当当: P2:正文 今天贴的是一个自定义的TextView,效果图如下: 看上去还是蛮炫目的对不对? 是不是觉得做了渐变的行为等等等之类的实现? 这里卖个关子,先介绍下如何使用,实现等会说一定让你吃鲸(懒人的创作,哈哈哈) Grade: de

Android中自定义TextView的样式

Android自定义TextView的样式,改变背景颜色,边框粗细和颜色,角的弧度等 在res/drawable文件夹下新建一个dd.xml文件,建一个shap,在里面添加需要改变的内容 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ef0909"></solid>//设置背景色 <strok