【问题标题】:Selecting text and changing attributes bug?选择文本和更改属性错误?
【发布时间】:2019-05-07 06:22:55
【问题描述】:

我在进行简单的文本选择和属性更改时遇到问题。我似乎无法让程序通过第一步。它说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 视图的 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 视图调用 bold() 时它打印 nil!我如何加载 AccessoryView。

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

【问题讨论】:

  • 您如何访问KeyboardAccessory 以及此let VC = ViewController() 创建此ViewController 的新实例,该实例与您正在访问的当前实例不同。
  • 好的。如果我在页面上创建一个新按钮,它就会起作用,所以这与之间的通信有关。我已经使用加载 AccessoryView 的代码对其进行了更新。非常感谢你的帮助! :)

标签: ios swift uitextview nsattributedstring


【解决方案1】:

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 编辑

更新您的键盘配件:

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?()
    }
}

尝试将您加载的笔尖转换为 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
}

【讨论】:

  • 好的!我有代表声明。我试图仅在用户单击按钮时更改它。具有将所选文本更改为粗体的按钮的示例。我需要 textViewDidChangeSelection 函数吗?
  • 你确定你的 textView 不是 nil 吗?因为我尝试在按钮操作中访问您的功能,并且根据您的方法正确更改了字体。
  • 是的,不是零。我打印了 - let textRange = textView?.selectedRange in textViewDidChangeSelection 并且它正在打印范围。是否需要在该函数内部调用?如果是这样,我如何从我的其他功能访问它?
  • 咳咳,我忘了: --> 应该是var boldAction: (() -> Void)?
  • 这是最简单的一种,那么您可能会根据自己的情况考虑更好的方法。也不要忘记将答案标记为已接受:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多