gpt4 book ai didi

ios - 选择文本和更改属性错误?

转载 作者:行者123 更新时间:2023-11-28 05:42:42 25 4
gpt4 key购买 nike

我在进行简单的文本选择和属性更改时遇到问题。我似乎无法让程序通过第一步。它表示 selectedRange 为零。为什么会这样?这是什么东西的错误吗?

func selectText() {
if let textRange = textView?.selectedRange {
let attributes = [NSAttributedString.Key.font: UIFont(name: "Arial", size: 18.0)]
textView.textStorage.addAttributes(attributes as [NSAttributedString.Key : Any], range: textRange)
}
}

使用来自@DionizB 的代码进行编辑(不工作)

我从另一个包含 KeyboardAccessory View 的 Swift 文件中调用它。该文件中的代码是:

class KeyboardAccessory: UIView {
let VC = ViewController()
@IBAction func boldButtonTapped(_ sender: Any) {
print("boldButtonTapped -> Sending to bold()")
VC.bold()
}
}

现在主 ViewController 中的代码是:

var selectedRange: NSRange?

func textViewDidChangeSelection(_ textView: UITextView) {
selectedRange = textView.selectedRange
print(selectedRange)
}

func bold() {
print("Starting bold()")
print(selectedRange)
if let textRange = selectedRange {
print(textRange)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.thin)]
textView.textStorage.addAttributes(attributes as [NSAttributedString.Key : Any], range: textRange)
}
}

textViewDidChangeSelection 正在打印 selectedRange,但是当从 KeyboardAccessory View 调用 bold() 时,它打印 nil!我如何加载 AccessoryView。

override func viewDidLoad() {
super.viewDidLoad()
textView.inputAccessoryView = Bundle.main.loadNibNamed("KeyboardAccessory", owner: self, options: nil)?.first as! UIView?
}

最佳答案

viewDidLoad() 上确保添加 textView.delegate = self。同样在您的类中继承自 UITextViewDelegate

然后在 textViewDidChangeSelection(_:) 中调用您的 selectText()

func textViewDidChangeSelection(_ textView: UITextView) {
selectText()
}

它会正常工作。

编辑通常,即使您在按钮操作中调用 selectText() ,它也应该可以工作。但由于它不起作用,让我们做一个解决方法:在您的类中声明 var selectedRange: NSRange?

然后在

func textViewDidChangeSelection(_ textView: UITextView) {
selectedRange = textView.selectedRange
}

然后在您的 selectText() 中执行此操作

func selectText() {
if let textRange = selectedRange {
let attributes = [NSAttributedString.Key.font: UIFont(name: "Arial", size: 18.0)]
textView.textStorage.addAttributes(attributes as [NSAttributedString.Key : Any], range: textRange)
}
}

为 AccessoryView 编辑

更新您的 KeyboardAccessory:

class KeyboardAccessory: UIView {
var boldAction: (() -> Void)? // This is a closure, give it a read after making your button work
@IBAction func boldButtonTapped(_ sender: Any) {
print("boldButtonTapped -> Sending to bold()")
boldAction?()
}
}

尝试将您加载的 Nib 转换为 KeyboardAccessory 并像这样访问您的操作:

override func viewDidLoad() {
super.viewDidLoad()
var keyboardAccessory = Bundle.main.loadNibNamed("KeyboardAccessory", owner: self, options: nil)?.first as! KeyboardAccessory?
keyboardAccessory.boldAction = { [weak self] in // to avoid retain cycles, read also about these
self?.selectText()
}
textView.inputAccessoryView = keyboardAccessory
}

关于ios - 选择文本和更改属性错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com