Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created April 2, 2022 22:07
Show Gist options
  • Save jakehawken/fdf71d1a108a51104ebcef5e727a86d6 to your computer and use it in GitHub Desktop.
Save jakehawken/fdf71d1a108a51104ebcef5e727a86d6 to your computer and use it in GitHub Desktop.
// InsetTextField.swift
// Created by Jacob Hawken on 4/2/22.
import UIKit
class InsetTextField: UITextField {
var leftInset: CGFloat = 8 { didSet { setNeedsLayout() } }
var rightInset: CGFloat = 8 { didSet { setNeedsLayout() } }
var topInset: CGFloat = 8 { didSet { setNeedsLayout() } }
var bottomInset: CGFloat = 8 { didSet { setNeedsLayout() } }
var textInset: UIEdgeInsets {
UIEdgeInsets(
top: topInset,
left: leftInset,
bottom: bottomInset,
right: rightInset
)
}
/// Rect for placeholder
override func textRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: textInset)
}
// Rect for typed text
override func editingRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: textInset)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment