textView在使用中通常会有2个功能是最常用的
- 设置placeholder
- 限制输入长度
TYLimitedTextView刚好是为了解决这个2个问题而诞生的,下面讲解TYLimitedTextView的用法,有需要的可以参考(textField限制输入请参考我的文章iOS textField输入限制),demo下载地址:https://github.com/qqcc1388/TYLimitInputDemo
TYLimitedTextView能够快速实现功能
- placeholoder功能实现
- textview输入长度实时监听,控制
- 提供代理方法实现textview return事件回调
话不多说,上代码
#import <UIKit/UIKit.h>
@protocol TYLimitedTextViewDelegate <NSObject>
@optional
/**
textView 键盘return事件监听
@param textView textView
*/
-(BOOL)limitedTextViewShouldReturn:(UITextView *)textView;
/**
textView内容改变实时监听
@param textView textView
*/
- (void)limitedTextViewDidChange:(UITextView *)textView;
/**
textView end editing
@param textView textView
*/
- (void)limitedTextViewDidEndEditing:(UITextView *)textView;
@end
@interface TYLimitedTextView : UITextView
@property (nonatomic,weak) id <TYLimitedTextViewDelegate> realDelegate;
/**
TYLimitedTextView允许输入的最大长度 默认 0不限制
*/
@property (nonatomic,assign) NSInteger maxLength;
/**
输入内容长度
*/
@property (nonatomic,assign) NSInteger inputLength;
@end
#import "TYLimitedTextView.h"
@interface TYLimitedTextView ()<UITextViewDelegate>
@end
@implementation TYLimitedTextView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self initialize];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self initialize];
}
return self;
}
-(void)initialize{
//设置默认值
self.maxLength = 0;
//设置基本属性
self.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
self.font = [UIFont systemFontOfSize:14];
self.backgroundColor = [UIColor whiteColor];
//设置代理
self.delegate = self;
}
#pragma mark - textViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//如果用户点击了return
if([text isEqualToString:@"\n"]){
if (_realDelegate && [_realDelegate respondsToSelector:@selector(limitedTextViewShouldReturn:)]) {
return [_realDelegate limitedTextViewShouldReturn:textView];
}
return NO;
}
//长度限制操作
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > self.maxLength && self.maxLength){
NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:self.maxLength];
if (rangeIndex.length == 1){//字数超限
textView.text = [str substringToIndex:self.maxLength];
//记录输入的字数
self.inputLength = textView.text.length;
if (_realDelegate && [_realDelegate respondsToSelector:@selector(limitedTextViewDidChange:)]) {
[_realDelegate limitedTextViewDidChange:textView];
}
}else{
NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.maxLength)];
textView.text = [str substringWithRange:rangeRange];
}
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length > self.maxLength && self.maxLength){
textView.text = [textView.text substringToIndex:self.maxLength];
}
//记录输入的字数
self.inputLength = textView.text.length;
if (_realDelegate && [_realDelegate respondsToSelector:@selector(limitedTextViewDidChange:)]) {
[_realDelegate limitedTextViewDidChange:textView];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView{
if (_realDelegate && [_realDelegate respondsToSelector:@selector(limitedTextViewDidEndEditing:)]) {
[_realDelegate limitedTextViewDidEndEditing:textView];
}
}
关于textView实现placeholder功能,这里通过UITextView+Placeholder分类来实现具体代码见demo
使用方法:
//设置允许输入的最大长度
self.textView.maxLength = 200;
//设置代理方法
self.textView.realDelegate = self;
self.textView.backgroundColor = [UIColor cyanColor];
//设置placeholder
self.textView.placeholder = @"请填写用户用户简介";
使用TYLimitedTextView一定要注意:TYLimitedTextView已经将系统的TextView部分代理方法拓展出来了,所有如果你需要使用TYLimitedTextView回调功能,请不要使用delegate而使用realDelegate,否则会出现冲突也就是self.delegate = self 的尴尬。如果拓展出来的方法不足以你使用,自己自行拓展。
转载请标注来源https://www.cnblogs.com/qqcc1388/p/7251946.html