鍵盤光標問題

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@""]) {
NSLog(@"刪除%@",textView.text);
return NO;
}else{
NSLog(@"---%@",text);
NSLog(@"%@",textView.text);
}
return YES;
}

/**

  • 光標位置輸入
  • @param emoji 要輸入的內(nèi)容emoji和字符
    */
  • (void)insertStringWithGuangBiaotext:(UITextView *)textView emoji:(NSString )emoji {
    NSRange range = textView.selectedRange;
    NSLog(@"%zd-光標位置輸入-%zd",range.length,range.location);
    NSUInteger location = textView.selectedRange.location;
    NSString * textStr = textView.text;
    NSString resultStr = [NSString stringWithFormat:@"%@%@%@",[textStr substringToIndex:location],emoji,[textStr substringFromIndex:location]];
    textView.text = resultStr;
    range.location = range.location + [emoji length];
    NSLog(@"%zd=%zd+%zd",range.location,range.location,[emoji length]);
    textView.selectedRange = NSMakeRange(range.location,0);
    }
    /
  • 光標位置刪除
    */
  • (void)deleteString:(UITextView *)textView {
    NSRange range = textView.selectedRange;
    NSLog(@"%zd-表情刪除-%zd",range.length,range.location);
    if (textView.text.length > 0) {
    NSUInteger location = textView.selectedRange.location;
    NSString *head = [textView.text substringToIndex:location];
    if (range.length ==0) {

      }else{
          NSLog(@"文字為全選");
          textView.text =@"";
      }
    
      if (location > 0) {
          //            NSUInteger location  = self.inputView.toolBar.textView.selectedRange.location;
          NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textView.text];
          [self lastRange:head];
          NSLog(@"%zd===%zd",[self lastRange:head].location,[self lastRange:head].length);
          NSLog(@"%@",str);
    
          [str deleteCharactersInRange:[self lastRange:head]];
    
          NSLog(@"%@",str);
          textView.text = str;
          textView.selectedRange = NSMakeRange([self lastRange:head].location,0);
    
      } else {
          textView.selectedRange = NSMakeRange(0,0);
      }
    

    }
    }

/**

  • 計算選中的最后一個是字符還是表情所占長度
  • @param str 要計算的字符串
  • @return 返回一個 NSRange
    */
  • (NSRange)lastRange:(NSString *)str {
    NSRange lastRange = [str rangeOfComposedCharacterSequenceAtIndex:str.length-1];
    return lastRange;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容