How to trigger an event when text is highlighted in swift

不羁的心 提交于 2019-12-11 16:59:30

问题


I want to make a book app in which the users will read the chapters in it. I want to add a feature in which the user can highlight text and keep it in the app for future reference. How can I trigger an event (like a button with an icon to highlight) after selecting text in swift? Please help

I have googled this for hours and nothing

//
//  ViewController.swift
//  platesofned
//
//  Created by Mauricio Kiyama on 5/13/19.
//  Copyright © 2019 Mauricio Kiyama. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let myTextView: UITextField = {
        let label = UITextField()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "Hello World"
        label.isSelected = true
        label.textColor = .black
        return label
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(myTextView)
        myTextView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        myTextView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        myTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        myTextView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true

        if let textRange = myTextView.selectedTextRange {

            let selectedText = myTextView.text(in: textRange)

            print(selectedText)
        }
    }


}

I want a box that has button after selecting any text.


回答1:


Assuming you are asking how to get an event when the selection changes in a UITextField, you need to add an observer to the "selectedTextRange" property of UITextField (from the UITextInput protocol).

Here is a little example view controller that listens to such events for a text field:

class MyVC: UIViewController {
    var textField: UITextField!

    @objc override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "selectedTextRange" {
            if let tf = object as? UITextField {
                // This is our event and text field
                if let range = tf.selectedTextRange {
                    print("Updated selection = \(range)")
                }

                return
            }
        }

        // This isn't the event or the object we care about, pass it on
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .yellow

        textField = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
        textField.text = "Hello there. How are you?"

        // Options is empty because we don't care about the old range and we can get the new range from the text field
        textField.addObserver(self, forKeyPath: "selectedTextRange", options: [], context: nil)

        view.addSubview(textField)
    }

    deinit {
        textField.removeObserver(self, forKeyPath: "selectedTextRange")
    }
}


来源:https://stackoverflow.com/questions/56120059/how-to-trigger-an-event-when-text-is-highlighted-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!