UITextView实现占位文字
来源:互联网 发布:视频教学软件 编辑:程序博客网 时间:2024/05/22 08:05
UITextField *textField默认的情况
1.只能显示一行文字
2.有占位文字
UITextView *textView默认的情况
2.能显示任意行文字
2.没有占位文字
文本输入控件,最终希望拥有的功能
1.能显示任意行文字
2.有占位文字
最终的方案:
1.继承自UITextView
2.在UITextView能显示任意行文字的基础上,增加”有占位文字”的功能
二、首先新建一个ZJPlaceholderTextView,.h文件如下
#import <UIKit/UIKit.h>@interface ZJPlaceholderTextView : UITextView/** 占位文字 */@property (nonatomic, copy) NSString *placeholder;/** 占位文字的颜色 */@property (nonatomic, strong) UIColor *placeholderColor;
第二步、.m文件如下:
#import "ZJPlaceholderTextView.h"@interface ZJPlaceholderTextView()/** 占位文字label */@property (nonatomic, weak) UILabel *placeholderLabel;@end@implementation ZJPlaceholderTextView- (UILabel *)placeholderLabel{ if (!_placeholderLabel) { // 添加一个用来显示占位文字的label UILabel *placeholderLabel = [[UILabel alloc] init]; placeholderLabel.numberOfLines = 0; placeholderLabel.x = 4; placeholderLabel.y = 7; [self addSubview:placeholderLabel]; _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]; // 监听文字改变 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil]; } return self;}- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}/** * 监听文字改变 */- (void)textDidChange{ // 只要有文字, 就隐藏占位文字label self.placeholderLabel.hidden = self.hasText;}/** * 更新占位文字的尺寸 */- (void)layoutSubviews{ [super layoutSubviews]; 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];}/** * setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法 * setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法 */@end
0 0
- UITextView实现占位文字
- iOS 实现 UITextView placeHolder的占位文字
- iOS UITextView的PlaceHolder 占位文字的实现
- iOS之UITextView实现placeHolder占位文字的最佳方法
- UITextView的Placeholder占位文字
- iOS-UITextView占位文字placeholder
- iOS之UITextView 实现占位符占位文字的Ñ种方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS开发:UITextView实现placeHolder占位文字的N种方法
- iOS 史上最全的 UITextView 实现 placeHolder 占位文字 的N种方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS 怎么给UITextView添加占位符文字
- ios中实现带占位符placeholder的UITextview
- 借用runtime来实现UITextView的占位符placehold功能
- 使用UITextView实现文字点击事件
- UITextView添加占位字符
- UITextView添加占位符
- XZ_iOS之使用textView实现占位文字
- 解决项目变砖的问题
- 计算机思维-最快的学习方法
- leetcode Substring with Concatenation of All Words
- 华为OJ题解(中级)
- HDU 4213 Bob’s Race(树形dp+单调队列)
- UITextView实现占位文字
- String str = new String("Hello"); 的内存分配
- android IPC机制(三)
- windows安装OracleXE112 64与PL/SQL连接详解
- SQL中JOIN和UNION区别、用法及示例介绍
- centos7安装emacs
- 论文笔记 | BoxSup: Exploiting Bounding Boxes to Supervise Convolutional Networks for Semantic Segmentati
- VPS CentOS 7搭建ss完整教程
- 调用相机并将照片存储到sd卡上