zoukankan      html  css  js  c++  java
  • (转载),方便使用http://www.cocoachina.com/bbs/read.php?tid=128244

    在textView中的光标位置插入表情或者文字   

     
     
    刚在写项目遇到在textView的光标位置插入表情,则查了查得出结论:
    方法一:

        int location  = contentText.selectedRange.location;
        NSString * textStr = contentText.text;
        NSString *str = [faceArr objectAtIndex:sender.tag];
        NSString *resultStr = [NSString stringWithFormat:@"%@%@%@",[textStr substringToIndex:location],str,[textStr substringFromIndex:location]];
        contentText.text = resultStr;

    方法二:

    //  将表情插入到当前光标

        NSString *str = [faceArr objectAtIndex:sender.tag];
        NSRange range = [contentText selectedRange];
        NSMutableString *top = [[NSMutableString alloc] initWithString:[contentText text]];
        NSString *addName = [NSString stringWithFormat:@"%@",str];
        [top insertString:addName atIndex:range.location];
        contentText.text = top;
        [top release];

    当然最后还有把光标置为 添加过内容的后面,所以:

    //  插入表情后 光标重新定位(延续方法二)

        NSUInteger length = range.location + [str length];
        contentText.selectedRange = NSMakeRange(length,0);
  • 相关阅读:
    如何处理消息堆积
    如何避免消息的重复发送
    内存泄漏和内存溢出的关系
    数据挖掘
    servlet
    数据驱动安全需三大核心新技术
    JS 入门经典 第三章 判断、循环和函数
    JS 高级程序设计 第三章
    JS入门经典
    JS高级程序设计1-2章
  • 原文地址:https://www.cnblogs.com/youmei11/p/4756628.html
Copyright © 2011-2022 走看看