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);
  • 相关阅读:
    用Visual C#创建Windows服务程序(转)
    输入字符串的格式不正确(异常详细信息: System.FormatException: 输入字符串的格式不正确。)
    pv操作二
    双进程struct sigaction中mask阻塞信号
    pv操作一
    sigprocmask
    共享内存二
    面向接口编程
    类之间的几种关系
    sigaction函数一
  • 原文地址:https://www.cnblogs.com/youmei11/p/4756628.html
Copyright © 2011-2022 走看看