限制UITextView的字数和字数监控,表情异常的情况和禁用表情
因为字数占一个字符,表情占两个字符。你要是限制15个字符的话,全打表情的话,最后一个表情会出现错误,所以要使用监听,对UITextViewTextDidChangeNotification监听,想用的可以直接拿来用,废话不多说,直接上代码!!UITextfield也是一样,只不过把监听写为UITextfieldTextDidChangeNotification
//移除监听
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:TtextView];
}
-(void)show{
//输入详细
TtextView = [[UITextView alloc]initWithFrame:CGRectMake(5*khScale, 5*khScale, self.view.frame.size.width-10*kScale, 160*khScale)];
TtextView.backgroundColor = [UIColor whiteColor];
TtextView.font = [UIFont systemFontOfSize:15*kScale];
TtextView.tag = 301;
TtextView.delegate = self;
[self.view addSubview:TtextView];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged9:) name:@"UITextViewTextDidChangeNotification" object:TtextView];
NSInteger textlength9;
- (void)textFiledEditChanged9:(UITextView *)textView {
NSString *toBeString = TtextView.text;
//获取高亮部分
UITextRange *selectedRange = [TtextView markedTextRange];
UITextPosition *position = [TtextView positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position)
{
if (toBeString.length > 200 && TtextView.markedTextRange == nil)
{
//用字符串的字符编码指定索引查找位置
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:200];
if (rangeIndex.length == 1)
{
TtextView.text = [toBeString substringToIndex:200];
}
else
{
//用字符串的字符编码指定区域段查找位置
// NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, Max_Length)];
TtextView.text = [toBeString substringWithRange:NSMakeRange(0, textlength9)];
}
} else {
textlength9 = toBeString.length;
}
}
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
_textNumberLabel.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)TtextView.text.length];
if (TtextView.text.length >=200) {
_textNumberLabel.textColor = [UIColor redColor];
}
else{
_textNumberLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
}
[self textChanged];
}
-(void)textChanged{
CGRect orgRect = TtextView.frame;//获取原始UITextView的frame
CGSize size = [TtextView sizeThatFits:CGSizeMake(TtextView.frame.size.width, MAXFLOAT)];
orgRect.size.height=size.height+10;//获取自适应文本内容高度
if (orgRect.size.height > 200) {
noteTextHeight = orgRect.size.height;
}
[self updateViewsFrame];
}
- (void)updateViewsFrame{
if (!allViewHeight) {
allViewHeight = 0;
}
if (!noteTextHeight) {
noteTextHeight = 100;
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
_textNumberLabel.text = [NSString stringWithFormat:@"%lu/200 ",(unsigned long)TtextView.text.length];
if (textView == TtextView) {
//这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
if (range.length == 1 && text.length == 0) {
return YES;
}
//so easy
else if (TtextView.text.length > 200) {
TtextView.text = [TtextView.text substringToIndex:200];
return NO;
}
}
return YES;
}
-(void)textViewDidChange:(UITextView *)textView
{
label.text = textView.text;
if (textView.text.length == 0) {
label.text = @"点击输入描述(200字以内)";
}else{
label.text = @"";
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
// 限制苹果系统输入法 禁止输入表情
if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
return NO;
}
return YES;
}