UITextView 光標(biāo)定位

在使用UITextView的時候, 如何在光標(biāo)的位置插入字符 或者 圖片? 以下Demo為你解答:

應(yīng)用背景:鍵盤自定義emoji表情

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
  //    NSString *newFaceName = change[@"new"];
      NSString *newFaceName = [change objectForKey:NSKeyValueChangeNewKey];
    
      if (!newFaceName || [newFaceName isKindOfClass:[NSNull class]]) {
          return;
      }
      // 在光標(biāo)位置插入表情
      [self textWithString:newFaceName];
//    [self attributedTextWithString:newFaceName];
    
}

#pragma mark - text && attributedText
- (void)textWithString:(NSString *)newFaceName
{

      // 1.1 獲取當(dāng)前輸入的文字
      NSMutableString *string = [NSMutableString stringWithString:_txView.text];
    
      // 1.2 獲取光標(biāo)位置
      NSRange rg = _txView.selectedRange;
      if (rg.location == NSNotFound) {
        
          // 如果沒找到光標(biāo),就把光標(biāo)定位到文字結(jié)尾
          rg.location = _txView.text.length;
      }
    
      // 1.3 替換選中文字
      [string replaceCharactersInRange:rg withString:newFaceName];
    
      _txView.text = string;
    
      // 1.4 定位光標(biāo)
      _txView.selectedRange = NSMakeRange(rg.location + newFaceName.length, 0);
}

// _txView.attributedText  && 雖然能在發(fā)送微博時顯示圖片
// 但是由于plist 文件中的 png名字與官方不一樣
// 所以發(fā)送出去的內(nèi)容微博不能識別 emoji 表情
- (void)attributedTextWithString:(NSString *)newFaceName
{

      // 1.1 獲取當(dāng)前輸入的文字
      NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
      // 1.1.1 拼接之前的文字(圖片和文字)
      [attributedText appendAttributedString:_txView.attributedText];
    
      // 1.2 獲取光標(biāo)位置
      NSRange rg = _txView.selectedRange;
      if (rg.location == NSNotFound) {
        
        // 如果沒找到光標(biāo),就把光標(biāo)定位到文字結(jié)尾
        rg.location = _txView.text.length;
      }
    
      // 1.3 替換選中文字
      // 1.3.1 加載圖片
      NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
      attachment.image = [UIImage imageNamed:newFaceName];
      CGFloat attchWH = _txView.font.lineHeight;
      attachment.bounds = CGRectMake(0, -3, attchWH, attchWH);
    
      NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment:attachment];
    
      // 1.3.2 拼接圖片
      [attributedText insertAttributedString:attributedString atIndex:rg.location];
    
      // 1.3.3 設(shè)置字體大小,_txView.font--> null ?!
  //    NSRange range = NSMakeRange(0, attributedText.length);
  //    [attributedText addAttribute:NSFontAttributeName value:_txView.font range:range];
    
      // 1.3.4 替換文字
      _txView.attributedText = attributedText;
    
      // 1.4 定位光標(biāo)
      _txView.selectedRange = NSMakeRange(rg.location + 1, 0);
}

利用KVO監(jiān)聽輸入的emoji表情

if (!_faceView) {
  _faceView = [[FaceView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];

  [_faceView.faceImgView addObserver:self forKeyPath:kFaceNameKVO options:NSKeyValueObservingOptionNew context:nil];
}

部分Demo:GitHub FaceViewDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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