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);
  • 相关阅读:
    Python学习【第五篇】:面向对象及相关
    Python之路【第四篇】:模块
    Python之路【第三篇】:python基础(二)
    Python之路【第二篇】:Python基础(一)
    Python之路【第一篇】:Python简介和入门
    Open-source Tutorial
    Algorithms
    Mathematics Base
    Mathematics Base
    Open-source Tutorial
  • 原文地址:https://www.cnblogs.com/youmei11/p/4756628.html
Copyright © 2011-2022 走看看