iOS自带的UITextView无法像UITextField一样设置placeholder属性,而现实中这种设置又比较常用,第一次写这玩意,有错误麻烦大家给指出,有更简便的实现望大家不吝赐教。本人菜鸟一枚。
#import <UIKit/UIKit.h>
/**
自定义带有plcaceholder的UITextView
*/
@interface RSPlaceholderTextView : UITextView
@property(nonatomic,copy) NSString* placeholder;
@property(nonatomic,strong) UIColor* placeholderColor;
@end
#import "RSPlaceholderTextView.h"
@interface RSPlaceholderTextView()<UITextViewDelegate>{
BOOL _showPlaceholder;
}
/**
保存外部代理
*/
@property(nonatomic,weak) id<UITextViewDelegate> rsDelegate;
@end
@implementation RSPlaceholderTextView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_showPlaceholder = YES;
super.delegate = self;
_placeholderColor = [UIColor colorWithRed:199/255.0 green:199/255.0 blue:205/255.0 alpha:1];
}
return self;
}
- (void)drawRect:(CGRect)rect{
if (_showPlaceholder && _placeholder) {
NSMutableDictionary* attibute = [NSMutableDictionary dictionary];
[attibute setObject:_placeholderColor forKey:NSForegroundColorAttributeName];
if (self.font) {
[attibute setObject:self.font forKey:NSFontAttributeName];
}
[_placeholder drawAtPoint:CGPointMake(self.textContainerInset.left+5, self.textContainerInset.top) withAttributes:attibute];
}
}
#pragma mark - setter
- (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)setDelegate:(id<UITextViewDelegate>)delegate{
_rsDelegate = delegate;
}
- (void)setText:(NSString *)text{
[super setText:text];
if ((text.length > 0 && _showPlaceholder) || (text.length == 0 &&
!_showPlaceholder)) {
_showPlaceholder = !_showPlaceholder;
[self setNeedsDisplay];
}
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if ([_rsDelegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
return [_rsDelegate textViewShouldBeginEditing:textView];
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
if ([_rsDelegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
return [_rsDelegate textViewShouldEndEditing:textView];
}
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
if ([_rsDelegate respondsToSelector:@selector(textViewDidBeginEditing:)]) {
[_rsDelegate textViewDidBeginEditing:textView];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView{
if ([_rsDelegate respondsToSelector:@selector(textViewDidEndEditing:)]) {
[_rsDelegate textViewDidEndEditing:textView];
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([_rsDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
return [_rsDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length > 0 && _showPlaceholder) {
_showPlaceholder = NO;
[self setNeedsDisplay];
}else if(textView.text.length == 0 && !_showPlaceholder){
_showPlaceholder = YES;
[self setNeedsDisplay];
}
if ([_rsDelegate respondsToSelector:@selector(textViewDidChange:)]) {
[_rsDelegate textViewDidChange:textView];
}
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
if ([_rsDelegate respondsToSelector:@selector(textViewDidChangeSelection:)]) {
[_rsDelegate textViewDidChangeSelection:textView];
}
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0){
if ([_rsDelegate respondsToSelector:@selector(textView:shouldInteractWithURL:inRange:interaction:)]) {
return [_rsDelegate textView:textView shouldInteractWithURL:URL inRange:characterRange interaction:interaction];
}
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0){
if ([_rsDelegate respondsToSelector:@selector(textView:shouldInteractWithTextAttachment:inRange:interaction:)]) {
return [_rsDelegate textView:textView shouldInteractWithTextAttachment:textAttachment inRange:characterRange interaction:interaction];
}
return YES;
}
@end
使用方法和UITextField一样
RSPlaceholderTextView* textView = [[RSPlaceholderTextView alloc] initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame)-20, 100)];
textView.font = [UIFont systemFontOfSize:20];
textView.placeholder = @"测试一下";
textView.delegate = self;
[self.view addSubview:textView];