Skip to content

Instantly share code, notes, and snippets.

@rajohns08
rajohns08 / LoadingButton.swift
Created November 13, 2015 04:55
iOS / Swift - UIButton subclass for showing loading spinner aka activity indicator inside button
import UIKit
class LoadingButton: UIButton {
var originalButtonText: String?
var activityIndicator: UIActivityIndicatorView!
func showLoading() {
originalButtonText = self.titleLabel?.text
self.setTitle("", forState: UIControlState.Normal)
@rajohns08
rajohns08 / docker.txt
Last active December 30, 2022 00:28
Docker - Popular Docker commands
# Show running containers
docker container ls
# Show all containers
docker container ls -a
# Create container from image
# -d run as daemon in background
# -p say what port to map in container
# --name give container a name
@rajohns08
rajohns08 / months.xml
Last active August 10, 2022 07:15
Android - string array for months
<string-array name="months_array">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
@rajohns08
rajohns08 / ssn.java
Last active November 30, 2021 08:50
Android - Add hyphens to an SSN EditText field while the user is typing
ssnEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// if user is typing string one character at a time
if (count == 1) {
@rajohns08
rajohns08 / HighlightButton.swift
Last active September 29, 2021 14:59
iOS / Swift - UIButton subclass to highlight when tapped
import UIKit
class HighlightButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setTitleColor(<#button text color when tapped#>, forState: .Highlighted)
}
override init(frame: CGRect) {
@rajohns08
rajohns08 / UITextFieldTestingTests.swift
Last active April 8, 2021 22:50
iOS / Swift - Unit test for verifying text field limit not exceeded
func testTextFieldLimit() {
// Set up view before interacting with the text field
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
vc.loadView()
// Test maximum number of allowable characters
let atTheLimitString = String(count: maxNumCharacters, repeatedValue: Character("a"))
let atTheLimitResult = vc.textField(vc.textField, shouldChangeCharactersInRange: NSRange(location: 0, length: 0), replacementString: atTheLimitString)
XCTAssertTrue(atTheLimitResult, "The text field should allow \(maxNumCharacters) characters")
@rajohns08
rajohns08 / FocusChangeEditText.java
Last active December 4, 2017 06:34
Android - Hide keyboard when tapping outside of an EditText
public class FocusChangeEditText extends android.support.v7.widget.AppCompatEditText {
public FocusChangeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
OnFocusChangeListener dismissKeyboardOnTapListener = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Keyboard.hide(v);
}
}
@rajohns08
rajohns08 / BorderButton.swift
Last active November 14, 2017 09:00
iOS / Swift - IBDesignable and IBInspectable working example for a UIButton subclass with a border
import UIKit
@IBDesignable
class BorderButton: UIButton {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
@rajohns08
rajohns08 / BaseUrl.swift
Created April 19, 2016 01:13
iOS / Swift - Base url class for getting from environment
class BaseURL {
class func getFromEnvironment() -> String {
#if DEBUG
return "http://192.168.1.126"
#else
return "https://judgecardx.com"
#endif
}
@rajohns08
rajohns08 / UIView.swift
Last active September 28, 2016 00:44
iOS / Swift - Autolayout wrapper to reduce boilerplate
extension UIView {
var differentSuperviewsWarningMessage: String {
return "Since you are adding a constraint to self.superview, self and the view passed in need to have the same superview. The views you are trying to align do not have the same superview."
}
func centerInSuperview() {
self.centerVerticallyInSuperview()
self.centerHorizontallyInSuperview()
}