iOS---自定义带占位文字placeholder的TextView
来源:互联网 发布:福彩3d矩阵图 编辑:程序博客网 时间:2024/06/05 17:23
本篇文章主要介绍了"ios 自定义带占位文字placeholder的TextView",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: 小伙伴们在写输入框的时候很可能需要带占位文字的TextView,由于TextField控件有占位文字的属性,但它不能多行显示,而TextView可以多行显示,但是它又没有占位文字属性,所以我们只能自定义TextView控件了:.
首先说一下思路:要定义这控件还是很简单,关键是一些细节上的处理问题(要让外部用起来很爽)。首先,让该控件继承自TextView,然后在这个TextView上加一个UILabel子控件,当用户点击TextView准备输入或TextView中有文字时,隐藏Label,没有文字时显示Label。当我们在监听TextView中的文字变化时,第一时间想到的肯定是设置代理,并让自己为代理,但这样的设计和苹果的代理模式相悖,能用但不合理这样设计不好,应该采取发通知的方式来监听TextView中的文字变化。
首先,初始化时加入Label:
- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; //添加一个显示占位文字的Label UILabel *placeholderLabel = [[UILabel alloc]init]; placeholderLabel.backgroundColor = [UIColor clearColor]; placeholderLabel.numberOfLines = 0; [self addSubview:placeholderLabel]; self.placeholderLabel = placeholderLabel; //设置占位文字颜色 self.placeholderColor = [UIColor lightGrayColor]; //设置默认字体 self.font = [UIFont systemFontOfSize:15]; //设置监听通知,不要设置自己的代理为本身 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self]; } return self;}
加入通知监听后,为防止内存泄露,要将其移除
/** * 移除通知监听 */- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}
布局子控件:
/** * 文字内容改变时调用的方法 */- (void)textChange { self.placeholderLabel.hidden = (self.text.length != 0);}/** * 布局子控件 */- (void)<a target=_blank href="http://www.codes51.com/article/search_layoutSubviews/" target="_blank" style="color: rgb(0, 0, 0); text-decoration: none;"><strong>layoutSubviews</strong></a> { [super <a target=_blank href="http://www.codes51.com/article/search_layoutSubviews/" target="_blank" style="color: rgb(0, 0, 0); text-decoration: none;"><strong>layoutSubviews</strong></a>]; CGFloat placeholderLabelX = 5; CGFloat placeholderLabelY = 8; CGFloat placeholderLabelWidth = self.frame.size.width - 2 * placeholderLabelX; CGFloat placeholderLabelHeight = [self.placeholder boundingRectWithSize:CGSizeMake(placeholderLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height; self.placeholderLabel.frame = CGRectMake(placeholderLabelX, placeholderLabelY, placeholderLabelWidth, placeholderLabelHeight);}- (void)setPlaceholder:(NSString *)placeholder { //copy策略的话setter应该这样写 _placeholder = [placeholder copy]; self.placeholderLabel.text = placeholder; //由于字符数变化,需要重新布局 [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 textChange];}
最后附上demo地址:http://download.csdn.net/detail/u013672551/9145287
以上就介绍了ios 自定义带占位文字placeholder的TextView,包括了方面的内容,希望对IOS开发有兴趣的朋友有所帮助。
0 0
- ios 自定义带占位文字(placeholder)的TextView
- iOS---自定义带占位文字placeholder的TextView
- 自定义带占位符Placeholder的textView
- iOS 实现 UITextView placeHolder的占位文字
- iOS-UITextView占位文字placeholder
- UITextView的Placeholder占位文字
- iOS UITextView的PlaceHolder 占位文字的实现
- iOS之UITextView实现placeHolder占位文字的最佳方法
- 自定义TextView,设置占位文字
- 猫猫学iOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- ios中实现带占位符placeholder的UITextview
- iOS占位文字placeholder设置技巧
- 自定义常用控件之~带placeholder的textview
- iOS自定义带有placeholder的TextView(多行)
- 自定义的TextView,带有placeholder
- 【iOS开发】设置textView的默认提示文字,也就是textView变相的"placeholder"。
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- urllib模块中的方法
- linux手动安装boost
- 三个油瓶分油问题
- 用STL中的map容器实现从很多单词找出所有只有一个字母不同的单词
- Cocoa框架图
- iOS---自定义带占位文字placeholder的TextView
- c++11 宽字节与窄字节的相互转换
- SpringIoC初始化BeanDefinition解析——注册
- NDK浅尝(1)
- c++11 时间戳
- 理解矩阵
- 富翁手机游戏开发实战----书中代码下载地址
- C语言--指针的使用
- CentOS6下SVN服务器的搭建