0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • maybe this can help https://stackoverflow.com/questions/26454037/uitextview-text-selection-and-highlight-jumping-in-ios-8, I can't think of anything really, maybe if you have some more code – Samuel Chavez May 13 '19 at 22:59
  • What text are you talking about? In the text field? So you want to know when the selection in the text field has changed? – rmaddy May 14 '19 at 00:07
  • Unrelated but it is really confusing to name a text field property as `myTextView` instead of `myTextField`. – rmaddy May 14 '19 at 00:08

1 Answers1

0

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")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579