TextKit中双向文本上左右字符的字符索引

时间:2019-02-01 03:55:48

标签: swift cocoa appkit nstextview textkit

我正在使用NSTextView子类,并且想知道当前插入点旁边的字符的字符索引。更具体地说,我想获取它们的字符索引,以使用箭头键移动光标,但不使用NSResponder的class MatrixSquare: def __init__(self): 'Contains primary shapes and partial or full constructed shapes' # initialize row, col self._row="" self._col="" self.ID=[self._row,self._col] @property def col(self): return self._col @property def row(self): return self._row @col.setter def col(self,col): self._col = col self.ID = [self._row,self._col] @row.setter def row(self,row): self._row = row self.ID = [self._row,self._col] moveLeft(_:)系列。

对于普通的水平单向文本,这很容易,因为moveRight(_:)是左边的文本,而index-1是右边的文本(只要您忘记字母由多个字符组成)。但是,通过混合使用从左到右和从右到左的混合文本,在视图中绘制的视觉字符顺序与实际字符索引不同。

السلام عليكم Hello السلم عليكم وHello

NSTextView的默认实现似乎以某种方式执行了双向文本。因此,我想获得与NSTextView的光标移动相同的行为,但是我找不到适合它的公共API。

到目前为止,我发现了NSLayoutManager的方法index+1似乎是获得比迪烟状况的合适方法。因此,我编写了如下扩展方法。但是,即使我可以得到特定位置的RTL状态,当索引位于RTL和LTR字母的边界时,我也不确定下一个字符的索引。

getGlyphs(in:glyphs:properties:characterIndexes:bidiLevels:)

我现在正在尝试的另一种方法是从视图坐标中获取它,如下所示。但是然后,该方法会跳过零宽度字符。

extension NSLayoutManager {

    func isRTL(at index: Int) -> Bool {

        let glyphIndex = self.glyphIndexForCharacter(at: index)

        guard glyphIndex > 0 else { return false }

        var bidiLevels: [UInt8] = [0]
        self.getGlyphs(in: NSRange((glyphIndex-1)..<glyphIndex), glyphs: nil, properties: nil, characterIndexes: nil, bidiLevels: &bidiLevels)

        return (bidiLevels[0] % 2 == 1)
    }
}

有人有满足我需求的想法吗?

0 个答案:

没有答案