/**
UITextView:
1.能显示任意行文字
2.不能设置占位文字
3.继承自UIScollView
4.监听行为
1> 设置代理
2> 通知:UITextViewTextDidChangeNotification
UITextField:
1.文字永远是一行,不能显示多行文字
2.有placehoder属性设置占位文字
3.继承自UIControl
4.监听行为
1> 设置代理
2> addTarget:action:forControlEvents:
3> 通知:UITextFieldTextDidChangeNotification
*/
主要步骤:
1.先自定义一个QTXTextView,便于自己的文本输入使用:
// 增强版自定义TextView:带有占位文字
#import <UIKit/UIKit.h>
@interface QTXTextView : UITextView
/** 占位文字 */
@property (nonatomic, copy) NSString *placeholder;
/** 占位文字的颜色 */
@property (nonatomic, strong) UIColor *placeholderColor;
@end
#import "QTXTextView.h"
@implementation QTXTextView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 不要设置自己的delegate为自己,在外部可能随时被别人覆盖掉
// self.delegate = self;
// 修改内部文字的上下左右间隔
self.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
// 通知
// 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知
[QTXNotificationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
// textview 改变字体的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 字体的行间距
paragraphStyle.lineSpacing = 10;
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:14],
NSParagraphStyleAttributeName:paragraphStyle
};
// 文字相关设置
self.attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:attributes];
self.textColor = setupTextLabelColor;
self.font = [UIFont systemFontOfSize:14];
}
return self;
}
- (void)dealloc {
[QTXNotificationCenter removeObserver:self];
}
/**
* 监听文字改变
*/
- (void)textDidChange {
// 重绘(重新调用)
[self setNeedsDisplay];
}
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text {
[super setText:text];
// setNeedsDisplay会在下一个消息循环时刻,调用drawRect:
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText {
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font {
[super setFont:font];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// 如果有输入文字,就直接返回,不画占位文字
if (self.hasText) return;
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font;
attrs[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : setupTextLabelColor;
// 画文字
CGFloat x = 15;
CGFloat w = rect.size.width - 2 * x;
CGFloat y = 15;
CGFloat h = rect.size.height - 2 * y;
CGRect placeholderRect = CGRectMake(x, y, w, h);
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
}
@end
2.在所需要使用的控制器里添加控件及代理
<UITextViewDelegate>
// 评价输入框
@property (nonatomic, weak) QTXTextView *commentTextView;
// 设置评价输入框
QTXTextView *commentTextView = [[QTXTextView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(line2.frame), UI_View_Width, 150)];
commentTextView.backgroundColor = [UIColor whiteColor];
commentTextView.delegate = self;
commentTextView.placeholder = @" 请留下您的宝贵意见,建议1~200字";
[self.bgView addSubview:commentTextView];
self.commentTextView = commentTextView;
#pragma mark- UITextViewDelegate
// 限制字数
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (range.location >= 200) {
return NO;
} else if ([text isEqualToString:@"\n"]) { // 点击return收起键盘
[textView resignFirstResponder];
return NO;
}
return YES;
}