闲暇时间自定义PNTextView : UITextView
,禁止输入空格、高度自适应、选择是否可输入表情符号等等
PNTextView--代码调用:
/// 姓名
PNTextView *textViewName = [[PNTextView alloc] init];
textViewName.font = [UIFont systemFontOfSize:18.0];
textViewName.placeholderText = @"请输入姓名";
textViewName.scrollEnabled = NO; // 重点
[self.view addSubview:textViewName];
self.textView = textViewName;
// textView与masonry绝配 高度不固定
[textViewName mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(150);
make.left.equalTo(self.view).mas_offset(10);
make.width.mas_offset(SCREEN_WIDTH - 10 * 2);
}];
// 其他属性
// 1、输入最大长度-多余5的字符与文字不可输入
textViewName.maxTextLength = 5;
// 2、是否可输入表情 默认是NO 可输入表情
textViewName.emoticonsDisEnable = YES;
PNTextView.h文件:
#import <UIKit/UIKit.h>
@interface PNTextView : UITextView
/// 最大输入个数
@property (nonatomic, assign) CGFloat maxTextLength;
/// 占位符颜色
@property (nonatomic, strong) UIColor *placeholderColor;
/// 占位符字体大小
@property (nonatomic, strong) UIFont *placeholderFont;
/// 占位字体内容
@property (nonatomic, copy) NSString *placeholderText;
/// 是否输入表情 默认是No
@property (nonatomic, assign, getter=isEmoticonsDisEnable) BOOL emoticonsDisEnable;
@end
PNTextView.m文件:
1、 初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.isContentTextViewEnable = YES;
self.delegate = self; // UITextViewDelegate
// 添加placeholderLabel
[self setupPlaceholderLabel];
}
return self;
}
/// 添加占位控件
- (void)setupPlaceholderLabel{
UILabel *placeholderLbl = [[UILabel alloc] init];
placeholderLbl.backgroundColor = [UIColor clearColor];
[self addSubview:placeholderLbl];
self.placeholderLbl = placeholderLbl;
self.maxTextLength = MAXFLOAT; // 默认个数不受限制
self.placeholderFont = [UIFont systemFontOfSize:16.0];
self.placeholderColor = [UIColor lightGrayColor];
self.font = self.placeholderFont; // 字体统一
}
2、实现方法
2.1 代理
#pragma mark - UITextViewDelegate
/// 输入内容
- (void)textViewDidChange:(UITextView *)textView
{
self.placeholderLbl.hidden = self.hasText;
if (!self.hasText) {
self.placeholderLbl.text = self.placeholderText; // 占位文字出现
return;
}
//获取当前键盘类型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];
//获取当前键盘语言
NSString *lang = mode.primaryLanguage;
//如果语言是汉语(拼音)
if ([lang isEqualToString:@"zh-Hans"])
{
//取到高亮部分范围
UITextRange *selectedRange = [self markedTextRange];
//取到高亮部分
UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
//如果取不到高亮部分,代表没有拼音,直接选择汉字
if (!position){
//当期超过最大限制时
if (self.text.length > self.maxTextLength) {
//对超出部分进行裁剪
self.text = [self.text substringToIndex:self.maxTextLength];
//同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
self.isContentTextViewEnable = NO;
[MBProgressHUD showError:@"输入汉字和字符个数已达上限"];
}
//可继续编辑
self.isContentTextViewEnable = YES;
}else{
//表示还有高亮部分,暂不处理
NSLog(@"输入拼音-高亮部分");
}
}else{
//如果语言不是汉语,直接计算, 输入英文字符情况
if (self.text.length > self.maxTextLength) {
NSLog(@"输入汉字和字符个数已达上限");
[MBProgressHUD showError:@"输入汉字和字符个数已达上限"];
self.text = [textView.text substringToIndex:self.maxTextLength];
self.isContentTextViewEnable = NO;
}
self.isContentTextViewEnable = YES;
}
}
/// 限制输入内容个数
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@""]) {
// 点击删除键 可删除
return YES;
}else if([text isEqualToString:@" "]){ // 空格,
return NO;
}else if([[self.textInputMode primaryLanguage] isEqualToString:@"emoji"] ||[self.textInputMode primaryLanguage] == nil){ // 表情:self.textInputMode.primaryLanguage是空
return !self.emoticonsDisEnable; // 表情可输入就返回yes,表情不可输入就返回no
}else if([text isEqualToString:@"\n"]){
return [self endEditing:YES]; // 退出键盘
}else{
return self.isContentTextViewEnable;
}
}
2.2 其他:利用通知监听语言的切换(代码中不需要写)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageChanged:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
/// 判断语言切换
-(void)languageChanged:(NSNotification*)notification
{
for(UITextInputMode *mode in [UITextInputMode activeInputModes])
{
NSLog(@"Input mode: %@", mode);
NSLog(@"Input language: %@", mode.primaryLanguage);
}
NSLog(@"Notification: %@", notification);
UITextInputMode *current = self.textView.textInputMode;
NSLog(@"Current: %@", current.primaryLanguage);
}
说明:通知记得在dealloc移除掉;
通知的这个方法是为了监听语言的切换,for循环中的所有语言分别打印如下所示:
所有语言
默认输入法是(我用的是苹果输入法):
"简体拼音"
切换为简体手写:
"简体手写"
切换为简体拼音:
"简体拼音"
切换为English打印:
"English(US)"
切换为表情之后的打印是:
"表情符号"
网友评论