//
//  NoCaretUITextField.swift
//  ABC Bluetooth Demo
//
//  Created by AB Circle on 23/9/2020.
//  Copyright © 2020-2024 AB Circle. All rights reserved.
//

import Foundation
import UIKit

/// Class overriding a UITextView in order to hide the caret and cancel edit actions.
/// Used for read-only text views with no default actions.
class NoCaretUITextView: UITextView {
    
    /// Returns a rectangle to draw the caret at a specified insertion point.
    /// Used here to hide the caret by returning a Zero CGRect.
    ///
    /// - Parameter position: An object that identifies a location in a text input area.
    /// - Returns: A rectangle that defines the area for drawing the caret.
    override func caretRect(for position: UITextPosition) -> CGRect {
        return .zero
    }
      
    /// Returns an array of selection rects corresponding to the range of text.
    /// Used here to hide text selection by returning an empty array
    ///
    /// - Parameter range: An object representing a range in a document’s text.
    /// - Returns: An array of UITextSelectionRect objects that encompass the selection.
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        return []
    }

    /// Requests the receiving responder to enable or disable the specified command in the user interface.
    /// Always returns false here so no actions are performable on this control
    ///
    /// - Parameters:
    ///   - action: A selector that identifies a method associated with a command.
    ///   - sender: The object calling this method.
    /// - Returns: true if the command identified by action should be enabled or false if it should be disabled.
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
    
    /// Returns a Boolean value indicating whether this object can become the first responder.
    /// Returns false always for this custom control
    override var canBecomeFirstResponder: Bool {
        return false
    }
}

