美文网首页
集成讯飞输入法的问题

集成讯飞输入法的问题

作者: okerivy | 来源:发表于2017-07-20 21:39 被阅读33次

    UITextview

    1, 在光标位置插入输入的文本

     NSUInteger location = self.selectedRange.location;
    

    2, 对textView赋值以后默认滚动到最后 滚动到光标位置代码为

     // 调整光标
        self.selectedRange = NSMakeRange(location + result.length, 0);
    // 滚动到光标位置
        [self scrollRangeToVisible:self.selectedRange];
    
    // 这个属性暂时记录, 没有用到 网上说有用 但是需要在viewdidappear
    //    self.textView.layoutManager.allowsNonContiguousLayout = NO;
    http://www.jianshu.com/p/1a647f65b1b6
    
    

    总的代码为

    -(void)voiceKeyboardDidRecognitionResult:(NSString *)result {
    
        
        // 获得光标所在的位置
        NSUInteger location = self.selectedRange.location;
        // 将UITextView中的内容进行调整(主要是在光标所在的位置进行字符串截取,再拼接你需要插入的文字即可)
        NSString *content = self.text;
        NSString *tempContent = [NSString stringWithFormat:@"%@%@%@",[content substringToIndex:location],result,[content substringFromIndex:location]];
        // 将调整后的字符串添加到UITextView上面
        self.text = tempContent;
        
        GFVoiceToolBar *toolbar = (GFVoiceToolBar *)self.inputAccessoryView;
        [toolbar setLabelText:[NSString stringWithFormat:@"已经输入 %zd 字", self.text.length]];
        
        // 调整光标
        self.selectedRange = NSMakeRange(location + result.length, 0);
        
        // 滚动到光标位置
        [self scrollRangeToVisible:self.selectedRange];
    
    }
    
    
    

    UITextField

    1, UITextField没有selectedRange属性
    所以创建分类 让他拥有

    UITextField+TKExtentRange.h

    #import <UIKit/UIKit.h>
    
    @interface UITextField (TKExtentRange)
    
    
    - (NSRange) selectedRange;
    - (void) setSelectedRange:(NSRange) range;
    
    
    @end
    
    
    

    UITextField+TKExtentRange.m

    
    #import "UITextField+TKExtentRange.h"
    
    @implementation UITextField (TKExtentRange)
    
    - (NSRange) selectedRange
    {
        UITextPosition* beginning = self.beginningOfDocument;
        
        UITextRange* selectedRange = self.selectedTextRange;
        UITextPosition* selectionStart = selectedRange.start;
        UITextPosition* selectionEnd = selectedRange.end;
        
        const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
        const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
        
        return NSMakeRange(location, length);
    }
    
    - (void) setSelectedRange:(NSRange) range  // 备注:UITextField必须为第一响应者才有效
    {
        UITextPosition* beginning = self.beginningOfDocument;
        
        UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
        UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
        UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
        
        [self setSelectedTextRange:selectionRange];
    }
    @end
    
    
    这样UITextField的光标使用方式就和UITextview相同了

    相关文章

      网友评论

          本文标题:集成讯飞输入法的问题

          本文链接:https://www.haomeiwen.com/subject/krkahxtx.html