HexEditUITextView

class HexEditUITextView : UITextView
extension HexEditUITextView: UITextViewDelegate

Class overriding an UITextView in order to enable hexadecimal input only. It uses a delegate function to check entered text and allows 0~9, space and A~F characters only. Also disabled the autocompletion feature that turns 2 spaces into a dot plus space.

  • Override initialiser.

    Declaration

    Swift

    override init(frame: CGRect, textContainer: NSTextContainer?)

    Parameters

    frame

    CGRect

    textContainer

    NSTextContainer

  • Required initialiser.

    Declaration

    Swift

    required init?(coder: NSCoder)

    Parameters

    coder

    NSCoder

  • Sets the delegate and initialises the text view borders

    Declaration

    Swift

    private func initialise()
  • Tells the delegate when the user changes the text or attributes in the specified text view. Used here to fix the “. ” back to double space

    Declaration

    Swift

    func textViewDidChange(_ textView: UITextView)

    Parameters

    textView

    The text view containing the changes.

  • Asks the delegate whether to replace the specified text in the text view. Used here to check if entered text conforms a characterset holding hexadecimal values + space.

    Declaration

    Swift

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool

    Parameters

    textView

    The text view containing the changes.

    range

    The current selection range.

    text

    The text to insert

    Return Value

    True if the old text should be replaced by the new text; false if the replacement operation should be aborted.