热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我怎样才能解决被分解为角色的表情符号?-HowcanIworkaroundtheemojibeingbrokenupintocharacters?

InmyiOSkeyboardextension,Ihaveafunctionthatmovesthecursorbackwardsasfollows:在我的iOS键

In my iOS keyboard extension, I have a function that moves the cursor backwards as follows:

在我的iOS键盘扩展中,我有一个向下移动光标的功能,如下所示:

(textDocumentProxy).adjustTextPositionByCharacterOffset(-1)

and displays the words right before the cursor in a UILabel using the textDocumentProxy.documentContextBeforeInput function provided by Apple. The problem is, whenever an emoji appears inside the label, it starts being separated as such: enter image description here

并使用Apple提供的textDocumentProxy.documentContextBeforeInput函数在UILabel中显示光标前的单词。问题是,只要表情符号出现在标签内部,它就会开始分离:

Is there any way of avoiding this? Does it have anything to do with me hardcoding the -1? I've tried using str.utf16.countbut that just doesn't scroll at all for some reason. Any help would be greatly appreciated.

有没有办法避免这种情况?这与我硬编码-1有什么关系吗?我已经尝试过使用str.utf16.count但由于某种原因根本不滚动。任何帮助将不胜感激。

Here's my question from a few months ago that never got answered

这是我几个月前的问题,从来没有得到答案

1 个解决方案

#1


1  

It appears that each offset will move the cursor by 16 bit and any characters with 32 bit will be cut in half. So the solution is for every 32 bit characters we move the cursor by 2 instead of 1.

看起来每个偏移都会将光标移动16位,任何32位字符都会被切成两半。因此解决方案是每32位字符,我们将光标移动2而不是1。

func moveCursorBy(offset: Int, onProxy proxy: UITextDocumentProxy){
    var realOffset = 0
    if offset <0, let text = proxy.documentContextBeforeInput {
        for uni in text.unicodeScalars.reverse().prefix(abs(offset)){
            realOffset -= uni.value > 0xFFFF ? 2 : 1
        }
    }
    else if offset > 0, let text = proxy.documentContextAfterInput{
        for uni in text.unicodeScalars.prefix(abs(offset)) {
            realOffset += uni.value > 0xFFFF ? 2 : 1
        }
    }
    else{
        realOffset = offset
    }
    proxy.adjustTextPositionByCharacterOffset(realOffset)
}

推荐阅读
author-avatar
陈雅杰昱宏
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有