123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // XLTextCalculateHelper.m
- // amigo
- //
- // Created by wujian on 2017/9/30.
- // Copyright © 2017年 xiaolian.Inc. All rights reserved.
- //
- #import "XLTextCalculateHelper.h"
- @implementation XLTextCalculateHelper
- + (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string textLimit:(NSUInteger)textLimit
- {
- UITextRange *selectedRange = [textField markedTextRange];
- //获取高亮部分
- UITextPosition *pos = [textField positionFromPosition:selectedRange.start offset:0];
- //获取高亮部分内容
- //NSString * selectedtext = [textView textInRange:selectedRange];
-
- //如果有高亮且当前字数开始位置小于最大限制时允许输入
- if (selectedRange && pos) {
- NSInteger startOffset = [textField offsetFromPosition:textField.beginningOfDocument toPosition:selectedRange.start];
- NSInteger endOffset = [textField offsetFromPosition:textField.beginningOfDocument toPosition:selectedRange.end];
- NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
-
- if (offsetRange.location < textLimit) {
- return YES;
- }
- else
- {
- return NO;
- }
- }
-
-
- NSString *comcatstr = [textField.text stringByReplacingCharactersInRange:range withString:string];
-
- NSInteger caninputlen = textLimit - comcatstr.length;
-
- if (caninputlen >= 0)
- {
- return YES;
- }
- else
- {
- NSInteger len = string.length + caninputlen;
- //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错
- NSRange rg = {0,MAX(len,0)};
-
- if (rg.length > 0)
- {
- NSString *s = @"";
- //判断是否只普通的字符或asc码(对于中文和表情返回NO)
- BOOL asc = [string canBeConvertedToEncoding:NSASCIIStringEncoding];
- if (asc) {
- s = [string substringWithRange:rg];//因为是ascii码直接取就可以了不会错
- }
- else
- {
- __block NSInteger idx = 0;
- __block NSString *trimString = @"";//截取出的字串
- //使用字符串遍历,这个方法能准确知道每个emoji是占一个unicode还是两个
- [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
- options:NSStringEnumerationByComposedCharacterSequences
- usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
-
- if (idx >= rg.length) {
- *stop = YES; //取出所需要就break,提高效率
- return ;
- }
- trimString = [trimString stringByAppendingString:substring];
- idx++;
- }];
- s = trimString;
- }
- //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件)
- [textField setText:[textField.text stringByReplacingCharactersInRange:range withString:s]];
- }
- return NO;
- }
- }
- + (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text textLimit:(NSUInteger)textLimit
- {
- UITextRange *selectedRange = [textView markedTextRange];
- //获取高亮部分
- UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
- //获取高亮部分内容
- //NSString * selectedtext = [textView textInRange:selectedRange];
-
- //如果有高亮且当前字数开始位置小于最大限制时允许输入
- if (selectedRange && pos) {
- NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
- NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
- NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
-
- if (offsetRange.location < textLimit) {
- return YES;
- }
- else
- {
- return NO;
- }
- }
-
-
- NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
-
- NSInteger caninputlen = textLimit - comcatstr.length;
-
- if (caninputlen >= 0)
- {
- return YES;
- }
- else
- {
- NSInteger len = text.length + caninputlen;
- //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错
- NSRange rg = {0,MAX(len,0)};
-
- if (rg.length > 0)
- {
- NSString *s = @"";
- //判断是否只普通的字符或asc码(对于中文和表情返回NO)
- BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding];
- if (asc) {
- s = [text substringWithRange:rg];//因为是ascii码直接取就可以了不会错
- }
- else
- {
- __block NSInteger idx = 0;
- __block NSString *trimString = @"";//截取出的字串
- //使用字符串遍历,这个方法能准确知道每个emoji是占一个unicode还是两个
- [text enumerateSubstringsInRange:NSMakeRange(0, [text length])
- options:NSStringEnumerationByComposedCharacterSequences
- usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
-
- if (idx >= rg.length) {
- *stop = YES; //取出所需要就break,提高效率
- return ;
- }
- trimString = [trimString stringByAppendingString:substring];
- idx++;
- }];
- s = trimString;
- }
- //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件)
- [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
- }
- return NO;
- }
- }
- + (BOOL)textFieldDidChangeWillContinue:(UITextField *)textField textLimit:(NSUInteger)textLimit
- {
- UITextRange *selectedRange = [textField markedTextRange];
- //获取高亮部分
- UITextPosition *pos = [textField positionFromPosition:selectedRange.start offset:0];
-
- //如果在变化中是高亮部分在变,就不要计算字符了
- if (selectedRange && pos) {
- return NO;
- }
-
- NSString *nsTextContent = textField.text;
- NSInteger existTextNum = nsTextContent.length;
-
- if (existTextNum > textLimit)
- {
- //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
- NSString *s = [nsTextContent substringToIndex:textLimit];
- [textField setText:s];
- }
- return YES;
- }
- + (BOOL)textViewDidChangeWillContinue:(UITextView *)textView textLimit:(NSUInteger)textLimit
- {
- UITextRange *selectedRange = [textView markedTextRange];
- //获取高亮部分
- UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
-
- //如果在变化中是高亮部分在变,就不要计算字符了
- if (selectedRange && pos) {
- return NO;
- }
-
- NSString *nsTextContent = textView.text;
- NSInteger existTextNum = nsTextContent.length;
-
- if (existTextNum > textLimit)
- {
- //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
- NSString *s = [nsTextContent substringToIndex:textLimit];
- [textView setText:s];
- }
- return YES;
- }
- @end
|