Skip to content

Instantly share code, notes, and snippets.

@MarcSteven
Created August 4, 2017 01:34
Show Gist options
  • Save MarcSteven/96b544723b09f31230749f0edc5d912c to your computer and use it in GitHub Desktop.
Save MarcSteven/96b544723b09f31230749f0edc5d912c to your computer and use it in GitHub Desktop.
//useful to construct the elements of UI fast ,we always write these code in the interface for instance
/*
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.adjustsFontSizeToFitWidth = adjustToFit
label.text = text
label.font = UIFont.preferredFont(forTextStyle: fontStyle)
label.textAlignment = .left
label.textColor = textColor
*/
//
// Factory.swift
// iReceiveFast
//
// Created by Marc Steven on 2017/8/3.
// Copyright © 2017年 Marc Zhao. All rights reserved.
//
import UIKit
/// button factory to construct the standard button
///
/// - buttonWithImage: the image of button
/// - sizeToFit:Bool: sizeToFit to make it display well
enum ButtonFactory {
case buttonWithImage(image:UIImage,cornerRadius:CGFloat,target:Any,selector:(Selector),sizeToFit:Bool)
var new:UIButton {
switch self {
case .buttonWithImage(let image, let cornerRadius,let target,let selector ,let sizeToFit):
return createButtonWithImage(image: image, cornerRadius: cornerRadius, target: target, selector: selector, sizeToFit: sizeToFit)
//Note:you can add the button with image based on your requirement here
}
}
fileprivate func createButtonWithImage(image:UIImage,cornerRadius:CGFloat,target:Any,selector:(Selector),sizeToFit:Bool) ->UIButton {
let button = UIButton()
button.setImage(image, for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.cornerRadius = cornerRadius
button.clipsToBounds = true
if sizeToFit {
button.sizeToFit()
}
return button
}
}
enum LabelFactory {
case standardLabel(text:String,textColor:UIColor,fontStyle:UIFontTextStyle,textAlignment:NSTextAlignment?,sizeToFit:Bool,adjustToFit:Bool)
var new:UILabel {
switch self {
case .standardLabel(let text, let textColor, let fontStyle, let textAlignment, let sizeToFit, let adjustToFit):
return createStandardLabel(text: text, textColor: textColor, fontStyle: fontStyle, textAlignment: textAlignment, sizeToFit: sizeToFit, adjustToFit: adjustToFit)
}
}
/// Construct standard label
///
/// - Parameters:
/// - text: text of label
/// - textColor: textColor
/// - fontStyle: fontStyle
/// - textAlignment: textAlignment
/// - sizeToFit: sizeToFit based on the content of label
/// - adjustToFit: adjustToFit to make it great
/// - Returns: <#return value description#>
fileprivate func createStandardLabel(text:String,textColor:UIColor,fontStyle:UIFontTextStyle,textAlignment:NSTextAlignment?,sizeToFit:Bool,adjustToFit:Bool) ->UILabel{
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.adjustsFontSizeToFitWidth = adjustToFit
label.text = text
label.font = UIFont.preferredFont(forTextStyle: fontStyle)
label.textAlignment = .left
label.textColor = textColor
if sizeToFit {
label.sizeToFit()
}
return label
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment