自定义UITextView
来源:互联网 发布:进销存财务一体软件 编辑:程序博客网 时间:2024/05/22 16:47
自定义UITextView,带有placeholeder,可以设置placeholeder文字的大小和颜色。
如图:
集成简单,只需在所用到的控制器中导入YMTextView即可,下面是示例代码:
在ViewController.m中
#import "ViewController.h"#import "YMTextView.h"@interface ViewController ()//输入控件@property (nonatomic, weak) YMTextView *textView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self setupLabel]; //设置输入控件 [self setupTextView];}-(void)setupLabel { UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 44)]; titleLabel.text = @"自定义textView控件"; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = [UIColor blackColor]; [self.view addSubview:titleLabel];}//添加输入控件-(void)setupTextView { // 在这个控制器中,textView的contentInset.top默认会等于64 YMTextView *textView = [[YMTextView alloc] init]; textView.frame = CGRectMake(0, 100, self.view.frame.size.width, 200); textView.backgroundColor = [UIColor lightGrayColor]; textView.font = [UIFont systemFontOfSize:15]; //设置占位文字 textView.placeholder = @"这里是占位文字..."; //设置占位文字颜色// textView.placeholderColor = [UIColor redColor]; [self.view addSubview:textView]; self.textView = textView; //监听通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView];}/** * 监听文字改变 */-(void)textDidChange { if (self.textView.hasText) { NSLog(@"文字发生改变----%@",self.textView.text); }}-(void)dealloc { //移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
在YMTextView.h中:
#import <UIKit/UIKit.h>@interface YMTextView : UITextView/** * 占位文字 */@property (nonatomic, copy) NSString *placeholder;/** * 占位文字颜色 */@property (nonatomic, strong) UIColor *placeholderColor;@end
在YMTextView.m中:
#import "YMTextView.h"@implementation YMTextView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; } return self;}/** * 监听文字改变 */-(void)textDidChange { //重绘 [self setNeedsDisplay];}-(void)setPlaceholder:(NSString *)placeholder { _placeholder = placeholder; // setNeedsDisplay会在下一个消息循环时刻,调用drawRect: [self setNeedsDisplay];}-(void)setText:(NSString *)text { [super setText:text]; // setNeedsDisplay会在下一个消息循环时刻,调用drawRect: [self setNeedsDisplay];}-(void)setFont:(UIFont *)font { [super setFont:font]; // setNeedsDisplay会在下一个消息循环时刻,调用drawRect: [self setNeedsDisplay];}- (void)drawRect:(CGRect)rect { // 如果有输入文字,就直接返回,不画占位文字 if (self.hasText) return; //设置文字属性 NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; attributes[NSFontAttributeName] = self.font; attributes[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : [UIColor grayColor]; //画文字 CGFloat x = 5; CGFloat width = rect.size.width -2 * x; CGFloat y = 8; CGFloat height = rect.size.height - 2 * y; CGRect placeholderRect = CGRectMake(x, y, width, height); [self.placeholder drawInRect:placeholderRect withAttributes:attributes];}-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];}@end
代码地址
0 0
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView控件
- 使用UITextView的问题及自定义UITextView
- iphone自定义UITextView的placeholder
- UITextView使用自定义表情键盘
- iOS学习之----------自定义UITextView
- UITextView自定义封装(带placeHolder)
- 自定义 UITextView 的 placeholder 属性
- UITextView自定义键盘和系统键盘
- UItextView自定义其高度,禁止滚动
- TextViewPlaceholder 自定义UITextView Placeholder文字颜色、大小
- ios-UITextview自定义 带placeholder,left间距
- UITextView
- UITextView
- UITextView
- ubuntu14.04安装Oracle_linux_x64_11gR2
- 使用Gradle构建Java项目
- 大型网站开发技术书籍汇总
- 23种设计模式全解析
- 算术操作符重载和关系操作符重载
- 自定义UITextView
- 新手学习记录4:makefile学习实践--伪目标的引入
- mysql explain的用法(使用explain优化查询语句)
- HDU 1166 线段树
- 中午乱码问题
- Windows Myeclipse 10 安装 Perl 插件
- 阿里云服务器环境搭建
- 如何在Windows Server中用命令行设置机器允许被远程?
- Scala之模式匹配和样例类