使用 Swift 选择 NSTextField 中的所有文本 [英] Select all text in a NSTextField using Swift

查看:64
本文介绍了使用 Swift 选择 NSTextField 中的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Swift 以编程方式选择 NSTextField 中的所有文本?

How can I programatically select all the text in a NSTextField using Swift?

对于 UITextField 有一个类似的方法

For UITextField theres a method like

textField.selectedTextRange = textField.textRangeFromPosition(...)

推荐答案

在操场上试试这个:

import XCPlayground
import AppKit

let view = NSView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

XCPShowView("view", view)

let txtf = NSTextField(frame: CGRect(x: 50, y: 50, width: 200, height: 50))

view.addSubview(txtf)

txtf.stringValue = "falling asleep at the keyboard..."

txtf.selectText(nil) // Ends editing and selects the entire contents of the text field

var txt = txtf.currentEditor() // returns an NSText

txt.selectedRange // --> (0,33)

Command-点击 selectedRange,然后点击 NSText(它的返回类型),这将跳转到它的 Swiftened 标头,在那里你可以查看它的丰富功能..

Command-click on selectedRange, then on NSText (its return type), which will jump you to its Swiftened header where you can check out its rich functionality...

这篇关于使用 Swift 选择 NSTextField 中的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆