UITextView自定义封装(带placeHolder)
2016-03-22 15:14
423 查看
根据我们的开发需求, 有时候UITextField不足以满足我们, 比如多行输入时, 不得不使用新的控件.
本篇文章对UITextView进行了重写和封装, 希望可以帮助大家.
重新定义封装UITextView
使用方法
常见问题
注意: PlaceHolderTextView为作者所起的名字, 可以随便叫什么
本篇文章对UITextView进行了重写和封装, 希望可以帮助大家.
重新定义封装UITextView
使用方法
常见问题
注意: PlaceHolderTextView为作者所起的名字, 可以随便叫什么
UITextView重写封装
/* .h文件 */ #import <UIKit/UIKit.h> @interface PlaceHolderTextView : UITextView @property (nonatomic, strong) NSString *placeholder; /* 灰色提示文字, 用于外部调用时添加的占位字符 */ @end
/* .m文件 */ #import "PlaceHolderTextView.h" @implementation PlaceHolderTextView - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self awakeFromNib]; } return self; } - (void)awakeFromNib { [self addObserver]; } #pragma mark 注册通知 - (void)addObserver { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self]; } #pragma mark 移除通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark 开始编辑 - (void)textDidBeginEditing:(NSNotification *)notification { if ([super.text isEqualToString:_placeholder]) { super.text = @""; [super setTextColor:[UIColor blackColor]]; } } #pragma mark 结束编辑 - (void)textDidEndEditing:(NSNotification *)notification { if (super.text.length == 0) { super.text = _placeholder; /* 如果文本框内是原本的提示文字,则显示灰色字体 */ [super setTextColor:[UIColor lightGrayColor]]; } } #pragma mark 重写setPlaceholder方法 - (void)setPlaceholder:(NSString *)aPlaceholder { _placeholder = aPlaceholder; [self textDidEndEditing:nil]; } #pragma mark 重写getText方法 - (NSString *)text { NSString *text = [super text]; if ([text isEqualToString:_placeholder]) { return @""; } return text; } @end
使用
#import "PlaceHolderTextView.h" /* 引入头文件 */
@property (strong, nonatomic) PlaceHolderTextView *feedBackTextView; /* 声明属性 */
self.feedBackTextView = [[PlaceHolderTextView alloc] initWithFrame:CGRectMake(10, 74, 355, 150)]; /* 给出尺寸 */ [self.view addSubview:self.feedBackTextView]; [self.feedBackTextView mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.top.equalTo(self.view.mas_top).with.offset(74); make.width.equalTo(@355); make.height.equalTo(@150); }];/* 添加约束 */ self.feedBackTextView.placeholder = @"亲, 您遇到什么系统问题啦, 或者有什么功能建议吗? 欢迎提给我们, 谢谢!"; /* 添加占位字符和TextField的placeholder 一样的使用方式 */
/* 如果需要圆角和边框这里设置 */ self.feedBackTextView.layer.borderWidth = 1.0; self.feedBackTextView.layer.borderColor = [UIColor grayColor].CGColor; /* 设置圆角 */ self.feedBackTextView.layer.cornerRadius = 5.0; self.feedBackTextView.font = [UIFont systemFontOfSize:17];
常见问题
比较常见的就是使用时 输入的文字居中显示 不像别人APP 中的靠上显示。/* 关闭视图的自适应就可以了 */ self.automaticallyAdjustsScrollViewInsets = NO;
相关文章推荐
- UIScrollView 基本属性
- configure: error: GD build test failed. Please check the config.log
- egret的eui以及exml使用
- hibernate3和hibernate4 query.setParameter(i,list.get(i))
- 只关心零件的Buider
- UIGestureRegnizer 的一些注意点
- Vue 技巧
- Android 学习笔记 Contacts (一)ContentResolver query 参数详解
- 【LeetCode】187. Repeated DNA Sequences
- 程序导入友盟微社区后报错android.view.InflateException:com.umeng.comm.ui.imagepicker.widgets.RoundImageView
- Server 2012 Core 到Full Gui的转换
- Activity中UI框架基本概念
- 修改EasyUI中DateBox和DateTimeBox的格式
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
- :The server quit without updating PID file问题的解决办法
- lintcode-medium-Implement Queue by Two Stacks
- 配置HUE 遇到Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found
- Java String、StringBuilder和StringBuffer
- Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案
- UIImageView或是UIButton怎样画环