ios - Swift 中的 UITextStorageDirection 和 UITextLayoutDirection 枚举是什么?

标签 ios swift uikit

谁能解释一下 Apple 的 UIKit 框架中的 UITextStorageDirectionUITextLayoutDirection 是什么,因为没有太多文档解释这两个枚举是什么,它们之间有什么区别?

另外,我想看一些关于文本字符串的例子,这两个枚举在给定的文本中代表什么?

最佳答案

UITextStorageDirection :- 文本存储的方向。

设置一个值,该值控制光标是显示在多行选择的最后一行的开头还是倒数第二行的结尾。

对于跨行边界的文本选择,此属性确定插入点是出现在该行最后一个字符之后还是下一行第一个字符之前。选择亲和性是响应于用户通过键盘导航(例如,命令右箭头)而设置的。文本输入系统在文档中移动插入点时会检查此属性。 - 案例

UITextStorageDirection.backward
UITextStorageDirection.forward

UITextLayoutDirection :- 文本布局的方向。

这个方法目前有点问题,因为它不需要 UITextLayoutDirectionUp 和 UITextLayoutDirectionDown 考虑在内。

这个补丁主要是为了修复一些“以防万一”的东西,因为 我们到目前为止还没有看到 UIKit 为那些调用这个方法 方向,除非连接了硬件键盘。而在 在这种情况下,我们无论如何都会通过处理来覆盖 IM 导航 使用明确的箭头键。

由于Qt中的IM不支持获取上面的位置 或者低于当前位置,我们只返回当前位置 位置,使其成为空操作。

Metadata ID

UIKit.UITextView.position(within: UIKit.UITextRange, farthestIn: UIKit.UITextLayoutDirection)

获取光标位置

if let selectedRange = textView.selectedTextRange {

    let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)

    print("\(cursorPosition)")
}

设置光标位置

为了设置位置,所有这些方法实际上都是设置一个具有相同起始值和结束值的范围。

到头

let newPosition = textView.beginningOfDocument
textView.selectedTextRange = textView.textRange(from: TextPosition, to: TextPosition)

到最后

let newPosition = textView.endOfDocument
textView.selectedTextRange = textView.textRangeFromPosition(newPosition, toPosition: newPosition)

到当前光标位置左侧的一个位置

// only if there is a currently selected range
if let selectedRange = textView.selectedTextRange {

    // and only if the new position is valid
    if let newPosition = textView.position(from: selectedRange.start in: UITextLayoutDirection.Left, offset: 1) {

        // set the new position
        textView.selectedTextRange = textView.textRange(from: TextPosition, to: TextPosition)
    }
}

到任意位置

从头开始向右移动 5 个字符。

let arbitraryValue: Int = 5
if let newPosition = 
textView.position(from: textView.beginningOfDocument in: UITextLayoutDirection.Right, offset: arbitraryValue) {

    textView.selectedTextRange = textView.textRange(from: TextPosition, to: TextPosition)
}

关于ios - Swift 中的 UITextStorageDirection 和 UITextLayoutDirection 枚举是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57457740/

相关文章:

swift - 调整 MapKit 搜索结果

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

php - 为什么 -stringWithContentsOfURL 不返回我的 php echo?

javascript - jqPlot 系列上的 jqplotClick 事件(iOS 设备 Safari 浏览器)

ios - 不断收到 "Unbalanced calls to begin/end appearance transitions for <ViewController>"错误

ios - 使用不同单位类型进行测量的最佳方法

ios - 在两个 Controller 之间发送 UIImage 是否正确?

ios - 屏幕尺寸减去广告尺寸

swift - 由于 'internal' 保护级别,“结果”无法访问

ios - 在 iOS 的 ViewController 中声明 View 时,为什么在 Swift 中使用 "weak"关键字