Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Created October 12, 2017 05:34
Show Gist options
  • Save anirudhamahale/7942e3f7339b3761af3aba9998e595cf to your computer and use it in GitHub Desktop.
Save anirudhamahale/7942e3f7339b3761af3aba9998e595cf to your computer and use it in GitHub Desktop.
Layout Services
//
// LayoutServices.swift
// AMLayout
//
// Created by Anirudha on 10/10/17.
// Copyright © 2017 Anirudha Mahale. All rights reserved.
//
import UIKit
class AMLayout {
static private let designedOnSize = CGSize(width: 375, height: 667)
static private let currentScreenSize = UIScreen.main.bounds.size
/// Use this method to adjust Font Size.
///
/// - Parameter constant: CGFloat
/// - Returns: Font size
static func getVerticleCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
return (AMLayout.currentScreenSize.height/AMLayout.designedOnSize.height)*constant
}
private static func reconfigureVerticleConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = AMLayout.getVerticleCostraintValueFrom(constraint.constant)
}
}
// Width
static func getHorizontalCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
return (AMLayout.currentScreenSize.height/AMLayout.designedOnSize.height)*constant
}
private static func reconfigureHorizontalConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = AMLayout.getHorizontalCostraintValueFrom(constraint.constant)
}
}
static func configureConstraints(_ vertical: [NSLayoutConstraint]?, horizontal: [NSLayoutConstraint]?) {
if let vertical = vertical {
AMLayout.reconfigureVerticleConstraints(vertical)
}
if let horizontal = horizontal {
AMLayout.reconfigureHorizontalConstraints(horizontal)
}
}
static private func configureTheConstraint(constraints: [NSLayoutConstraint]) {
for constraint in constraints {
if constraint.firstAttribute == .height {
constraint.constant = AMLayout.getHorizontalCostraintValueFrom(constraint.constant)
} else if constraint.firstAttribute == .width {
constraint.constant = AMLayout.getVerticleCostraintValueFrom(constraint.constant)
}
}
}
static func configureConstraintOf(_ views: [Any]) {
for view in views {
if let button = view as? UIButton {
configureTheConstraint(constraints: button.constraints)
continue
}
if let imageView = view as? UIImageView {
configureTheConstraint(constraints: imageView.constraints)
continue
}
if let textField = view as? UITextField {
configureTheConstraint(constraints: textField.constraints)
continue
}
if let textView = view as? UITextView {
configureTheConstraint(constraints: textView.constraints)
continue
}
if let baseView = view as? UIView {
configureTheConstraint(constraints: baseView.constraints)
continue
}
}
}
static func configureFontSize(_ views: [Any]) {
for view in views {
if let label = view as? UILabel {
label.font = label.font.withSize(AMLayout.getVerticleCostraintValueFrom(label.font.pointSize))
continue
}
if let textField = view as? UITextField {
textField.font = textField.font?.withSize(AMLayout.getVerticleCostraintValueFrom(textField.font!.pointSize))
continue
}
if let textView = view as? UITextView {
textView.font = textView.font?.withSize(AMLayout.getVerticleCostraintValueFrom(textView.font!.pointSize))
continue
}
if let button = view as? UIButton {
button.titleLabel?.font = button.titleLabel?.font.withSize(AMLayout.getVerticleCostraintValueFrom(button.titleLabel!.font.pointSize))
continue
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment