Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created July 19, 2017 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcdilorenzo/bdd344aeb63980cfee2f2112a7fca851 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/bdd344aeb63980cfee2f2112a7fca851 to your computer and use it in GitHub Desktop.
Various iOS extensions for creating reusable views in code
import UIKit
extension UIButton {
static func createBordered(light: Bool) -> UIButton {
let button = UIButton(type: .roundedRect)
button.contentEdgeInsets = UIEdgeInsets(top: 5, left: 7, bottom: 5, right: 7)
button.layer.cornerRadius = 5
button.translatesAutoresizingMaskIntoConstraints = false
if (light) {
button.backgroundColor = UIColor.white
button.setTitleColor(UIColor.primary, for: .normal)
}
return button
}
}
import UIKit
extension UILabel {
static func createTitle(text: String, light: Bool, oversized: Bool) -> UILabel {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.clear
label.textColor = light ? UIColor.white : UIColor.primary
label.font = UIFont.systemFont(ofSize: oversized ? 36 : 22, weight: UIFontWeightThin)
label.adjustsFontForContentSizeCategory = true
label.text = text
return label
}
}
import UIKit
extension UITextField {
static func createDefault(_ placeholder: String?) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.backgroundColor = UIColor.white
textField.placeholder = placeholder
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}
static func createPassword(_ placeholder: String?) -> UITextField {
let textField = createDefault(placeholder)
textField.isSecureTextEntry = true
return textField
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment