zoukankan      html  css  js  c++  java
  • 第42月第18天 iOS匹配特殊字符 markedTextRange

    1.

    - (BOOL)checkSpecialCharacter:(NSString *)string
    {
        NSString *regex = @"[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]+";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pred evaluateWithObject:string];
    }
    

      https://www.jianshu.com/p/88be28860cde

        __block BOOL isMatch = NO;
        NSString *letters = @"(  ˃᷄˶˶̫˶˂᷅  )౿(།﹏།)૭ᕙ(•̤᷆ ॒ ູ॒•̤᷇)ᕘ_(___°π°œ)_「」°|_|_=3( ´・ᴗ・` )*/ω\*)£";
        [letters enumerateSubstringsInRange:NSMakeRange(0, [letters length])
                                    options:(NSStringEnumerationByComposedCharacterSequences)
                                 usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            if ([string containsString:substring]) {
                NSLog(@"match character:%@",substring);
                isMatch = YES;
                *stop = YES;
            }
        }];
        
        if (!isMatch) {
            NSString *pattern = @"[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]+";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
            isMatch = [pred evaluateWithObject:string];
        }
        NSLog(@"%@ is SpecialCharacters:%d",string,isMatch);
    - (NSInteger)curOffset{
     
        // 基于文首计算出到光标的偏移数值。
        return [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start];
     
    }
     
    - (void)makeOffset:(NSInteger)offset{
     
        // 实现原理是先获取一个基于文尾的偏移,然后加上要施加的偏移,再重新根据文尾计算位置,最后利用选取来实现光标定位。
        UITextRange *selectedRange = [self selectedTextRange];
        NSInteger currentOffset = [self offsetFromPosition:self.endOfDocument toPosition:selectedRange.end];
        currentOffset += offset;
        UITextPosition *newPos = [self positionFromPosition:self.endOfDocument offset:currentOffset];
        self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];
     
    }
     
    - (void)makeOffsetFromBeginning:(NSInteger)offset{
     
        // 先把光标移动到文首,然后再调用上面实现的偏移函数。
        UITextPosition *begin = self.beginningOfDocument;
        UITextPosition *start = [self positionFromPosition:begin offset:0];
        UITextRange *range = [self textRangeFromPosition:start toPosition:start];
        [self setSelectedTextRange:range];
        [self makeOffset:offset];
     
    }

    https://blog.csdn.net/zhanglizhi111/article/details/71411584

    2.

    - (void)textViewDidChange:(UITextView *)textView {
        if (self.titleTextView.markedTextRange == nil && [TCUtil getContentLength:self.titleTextView.text] > kTCMaxPushTitleLen) {      
            [[HUDHelper sharedInstance] tipMessage:@"已达到最大限制字数"];
            self.titleTextView.text = [self.titleTextView.text substringToIndex:(self.titleTextView.text.length-1)];
            
            [textView resignFirstResponder];
        }
        
    }
    //通过分别计算中文和其他字符来计算长度
    + (NSUInteger)getContentLength:(NSString*)content {
        size_t length = 0;
        for (int i = 0; i < [content length]; i++)
        {
            unichar ch = [content characterAtIndex:i];
            if (0x4e00 < ch  && ch < 0x9fff)
            {
                length += 2;
            }
            else
            {
                length++;
            }
        }
        
        return length;
    }

    https://www.github.com/tencentyun/MLVBSDK/

    - (void)notifyTextChange:(NSNotification *)n
    {
        if ( self.maxInputLength == 0 )
        {
            return;
        }
        
        if ( n.object != self.inputView )
        {
            return;
        }
        
        UITextField *textField = self.inputView;
        
        NSString *toBeString = textField.text;
    
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > self.maxInputLength) {
                textField.text = [toBeString mm_truncateByCharLength:self.maxInputLength];
            }
        }
    }
    - (NSString *)mm_truncateByCharLength:(NSUInteger)charLength
    {
        __block NSUInteger length = 0;
        [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                                 options:NSStringEnumerationByComposedCharacterSequences
                              usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                  
                                  if ( length+substringRange.length > charLength )
                                  {
                                      *stop = YES;
                                      return;
                                  }
                                  
                                  length+=substringRange.length;
                              }];
        
        return [self substringToIndex:length];
    }

    https://github.com/CoderLT/MMPopupView

    3.

    uploadUrl = [uploadUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

  • 相关阅读:
    2020寒假简记
    感知神经网络模型与学习算法
    信息检索模型与评估
    Diffie-Hellman密钥交换
    RSA密码体制
    MySQL基准测试(benchmark)
    MySQL数据引擎
    MySQL 多版本并发控制(MVCC)
    MySQL事务管理
    利用dotnet restore 导入本地 .nupkg 包
  • 原文地址:https://www.cnblogs.com/javastart/p/12519702.html
Copyright © 2011-2022 走看看