-在开发中实现文本的输入方式有UITextField和UITextView
- UITextField默认有占位文字,但最多只能输入一行文字
- UITextView能输入多行文字,但没有占位文字
我们在开发中往往需要既有占位文字又需要多行文字,这就需要我们自定义来解决了 - 在这里采用继承自UITextView来增加占位文字
-具体的代码实现
- 在自定义的ZBHTextView的 .h 中
#import <UIKit/UIKit.h>
@interface ZBHTextView : UITextView
/** 占位文字 */
@property(nonatomic, copy) NSString *placeholder;
/** 占位文字颜色 */
@property(nonatomic, strong) UIColor *placeholderColor;
@end
继承自UITextView,提供placeholder占位文字和placeholderColor占位文字颜色两个属性
- 在ZBHTextView的 .m 中
#import "ZBHTextView.h"
@implementation ZBHTextView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置默认字体
self.font = [UIFont systemFontOfSize:17];
// 设置默认颜色
self.placeholderColor = [UIColor grayColor];
// 使用通知监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)textDidChange:(NSNotification *)note
{
// 会重新调用drawRect:方法
[self setNeedsDisplay];
}
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 每次调用drawRect:方法,都会将以前画的东西清除掉
- (void)drawRect:(CGRect)rect
{
// 如果有文字,就直接返回,不需要画占位文字
if (self.hasText) return;
// 属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font;
attrs[NSForegroundColorAttributeName] = self.placeholderColor;
// 画文字 光标与文字不一致,设置尺寸
rect.origin.x = 5;
rect.origin.y = 8;
rect.size.width -= 2 * rect.origin.x;
[self.placeholder drawInRect:rect withAttributes:attrs];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self setNeedsDisplay];
}
#pragma mark - setter
//重写setter方法调用setNeedsDisplay方法刷新
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
@end
在这里是通过drawRect方法来写入文字,并且在文字发生改变的时候调用setNeedsDisplay来刷新
- 在ViewController.m中的代码代码实现
#import "ViewController.h"
#import "ZBHTextView.h"
@interface ViewController ()<UITextViewDelegate>
/** 自定义的textView */
@property(nonatomic, strong) ZBHTextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//导航条内容
[self setupNav];
//添加文本框
[self setupTextView];
}
#pragma mark -- 初始化
//导航条内容
- (void)setupNav{
self.title = @"发文字";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStyleDone target:self action:@selector(post)];
self.navigationItem.rightBarButtonItem.enabled = NO;
//强制刷新
[self.navigationController.navigationBar layoutIfNeeded];
}
- (void)setupTextView{
ZBHTextView *textView = [[ZBHTextView alloc] init];
textView.frame = self.view.bounds;
//设置可以上下拖动
textView.alwaysBounceVertical = YES;
textView.delegate = self;
textView.placeholder = @"我是占位文字....我是占位文字....我是占位文字....我是占位文字....我是占位文字....我是占位文字....哈哈哈";
[self.view addSubview:textView];
self.textView = textView;
}
- (void)cancel{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)post{
//发表
}
#pragma mark -- UITextFieldDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//拖拽时 结束编辑 退出键盘
[self.textView endEditing:YES];
}
- (void)textViewDidChange:(UITextView *)textView{
//判读是否有文字 设置发表按钮的状态
self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}
@end
在ViewController中只需要调用textView.placeholder方法就可以实现占位文字