Skip to content

Instantly share code, notes, and snippets.

@austinzheng
Last active January 20, 2022 15:14
Show Gist options
  • Select an option

  • Save austinzheng/a8563c6babfd61401be3 to your computer and use it in GitHub Desktop.

Select an option

Save austinzheng/a8563c6babfd61401be3 to your computer and use it in GitHub Desktop.

Revisions

  1. austinzheng revised this gist Jun 26, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions CustomTextInputView.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    import UIKit

    /// A very simple example view that can accept keyboard input and add or delete text from an enclosed label.
    class CustomTextInputView : UIControl, UIKeyInput {

    var label : UILabel?
    @@ -15,6 +16,7 @@ class CustomTextInputView : UIControl, UIKeyInput {
    }

    @objc private func onTap(_: AnyObject) {
    // When the view is tapped, it becomes the first responder. If the keyboard is not already on screen, it will appear.
    becomeFirstResponder()
    }

    @@ -24,6 +26,7 @@ class CustomTextInputView : UIControl, UIKeyInput {
    addTarget(self, action: Selector("onTap:"), forControlEvents: .TouchUpInside)
    }

    /// Programmatically create and configure the enclosed label.
    private func build() {
    let lbl = UILabel()
    lbl.setTranslatesAutoresizingMaskIntoConstraints(false)
  2. austinzheng created this gist Jun 26, 2015.
    58 changes: 58 additions & 0 deletions CustomTextInputView.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    import UIKit

    class CustomTextInputView : UIControl, UIKeyInput {

    var label : UILabel?

    override func canBecomeFirstResponder() -> Bool {
    return true
    }

    init() {
    super.init(frame: CGRectZero)
    build()
    addTarget(self, action: Selector("onTap:"), forControlEvents: .TouchUpInside)
    }

    @objc private func onTap(_: AnyObject) {
    becomeFirstResponder()
    }

    required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    build()
    addTarget(self, action: Selector("onTap:"), forControlEvents: .TouchUpInside)
    }

    private func build() {
    let lbl = UILabel()
    lbl.setTranslatesAutoresizingMaskIntoConstraints(false)
    lbl.numberOfLines = 0
    addSubview(lbl)
    lbl.font = UIFont(name: "HelveticaNeue-Light", size: 14.0)!

    // Constrain!
    let views = ["lbl": lbl]
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[lbl]-|", options: nil, metrics: nil, views: views))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[lbl]-|", options: nil, metrics: nil, views: views))
    label = lbl
    }


    // MARK: UIKeyInput

    func hasText() -> Bool {
    return label?.text?.isEmpty ?? false
    }

    func insertText(text: String) {
    label?.text = (label?.text ?? "") + text
    }

    func deleteBackward() {
    if let text = label?.text where !text.isEmpty {
    let newText = text[text.startIndex..<text.endIndex.predecessor()]
    label?.text = newText
    }
    }
    }