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

标签 ios swift uitextview nsattributedstring

我在进行简单的文本选择和属性更改时遇到问题。我似乎无法让程序通过第一步。它表示 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/

相关文章:

ios - 如何在 UIWebView 而不是 Safari 中打开 UITextView 网络链接?

ios - 更改了子字符串的颜色,但字体设置已重置

ios - 未显示 UIWebView

ios - Xcode Application Loader 停留在 "Signing in to App Store Connect"

iphone - 在 UITableViewCell 中设置图像时,出现 <Error> : CGAffineTransformInvert: singular matrix

ios - 旋转时,文本在 UITextView 行尾之前断到下一行

iphone - 在 objective-c 中使用 BOOL 变量时发出警告

ios - Swift: 'Tap to Begin' 按钮 - 删除自身

ios - 更改选项卡时 Web View 会使选项卡式应用程序崩溃?

ios - 发现不支持的类型 - AVMetadataObject