【问题标题】:NativeScript - How to get the caret position of TextView in iOSNativeScript - 如何在 iOS 中获取 TextView 的插入符号位置
【发布时间】:2019-03-30 18:34:40
【问题描述】:

我正在使用 NativeScript-Vue,但在 iOS 中查找 TextView 的插入符号位置时遇到问题

<StackLayout>
    <TextView ref="input" editable=true @loaded="getView"/>
</StackLayout>

...

getView(args) {
    console.log(args.object.ios)      // return {}
    console.log(this.$refs.input.ios) // return {}
}

...

https://discourse.nativescript.org/t/getting-cursor-position-of-textview-in-ios/5931

我已经阅读了上面的解决方案,但似乎只支持 TypeScript?

我从this.$refs.input.nativeView.ios 得到的总是{}(空)

有人知道我错过了什么吗?或者欢迎任何可用的方法!干杯

【问题讨论】:

    标签: ios textview nativescript caret


    【解决方案1】:

    你必须只使用this.$refs.input.nativeView.ios,它可能会打印空对象,因为它是一个原生组件。

    【讨论】:

    • 感谢您的回复,但很抱歉@Manoj,我真的不明白您的意思,那么我怎样才能获得beginningOfDocument?我做了console.log(this.$refs.input.nativeView.ios.beginningOfDocument 并且还返回了一个空的obj,它是本机组件是否正常?
    • 您只能在控制台中转储 JavaScript 对象的所有可用属性或方法,本机对象不能被视为仅返回空对象但仍然可以工作的方式。这是一个工作示例 - play.nativescript.org/?template=play-vue&id=q1LnDV 如果您使用 TypeScript,您可以使用 tns-platform-declrations 包,该包将帮助您列出本机对象上所有可用的本机方法。
    【解决方案2】:

    我创建了一个函数,允许我获取选定的文本。通过这个函数,您还可以确定插入符号的位置。

    function getSelectedText(view) {
      let selection = {
        start: view.ios.selectedRange.location,
        end: view.ios.selectedRange.location + view.ios.selectedRange.length,
        length: view.ios.selectedRange.length
      }
      
      return selection;
    }
    

    要使用此功能,只需获取您的文本字段的引用:

    getSelectedText(this.$refs.textField.nativeView);
    

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2011-06-23
      • 2017-12-15
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多