Giter VIP home page Giter VIP logo

backbone's Introduction

BackBone

When I start a project, I notice that I always setup it in the same way, and setupping it steals me a lot of time because maybe I have to copy code from my latest projects, getting the latest optimizations.

So I decided to create a repo, always updated, with the most used libraries and my directories.

I splitted it in 3 main branches:

  1. Without API Client
  2. With Alamofire
  3. With Apollo Client (GraphQL)

These 3 branches have the same core, based on files grouped by semantic (I.E. UIViewControllers/...)

#Style with UIAppearance I created a solid logic based on UIAppearance, so I subclass every primitive UIKit element (UILabel, UIButton etc.) and then I apply a specific style for the sublcass.

Example

I want to have a label which has red textColor and a blue backgroundColor.

  1. Create a Swift file (if doesn't already exist) in Subclasses directory called UILabels.

  2. If it's the first label that you add as subclass, first create a class in UILabels.swift in this way: @IBDesignable class DefaultLabel{}. It's important because UIAppearance overrides every UILabel style, like titleLabel in UIButtons.

  3. Inside that file, add a class:@IBDesignable class RedOnBlueLabel:DefaultLabel{}

  4. Check if exists a file in Themes directory named ThemeLabels.swift or something like that. If not, create that

  5. Add a struct called as you want (I suggest something like ThemeLabel

  6. Add a function called static func setStyle(for theme:Theme)

  7. The enum Theme is an enum which you define in file Themes/Theme.swift

  8. If your app could be used in different modes (like standard and dark), your function added in point 6 will be:

		static func setStyle(for theme:Theme){
   			switch theme {
        	case .standard:
				DefaultLabel.appearance().textColor = UIColor.black
				RedOnBlueLabel.appearance().textColor = UIColor.red
				RedOnBlueLabel.appearance().backgroundColor = UIColor.blue
			case .dark:
				DefaultLabel.appearance().textColor = UIColor.white
				RedOnBlueLabel.appearance().textColor = UIColor.red
				RedOnBlueLabel.appearance().backgroundColor = UIColor.blue
			}
		}
  1. In Themes/Theme.swift, would be:
enum Theme{
    case standard
    case dark
    
    func setStyle(){
        ThemeLabel.setStyle(for:self)
    }
}

  1. That's it! This setStyle() is called in AppDelegate->DidFinishLaunchingWithOptions function.

Add specific UIAppearance properties

You can also add particular properties, for instance in a custom UIView which you want to customize in UIAppearance. Or something that you normally can't customize through UIAppearance.

###Example

Assuming that you want to set UILabel's height in UIAppearance. Inside your subclass declaration, add the following code:

    var heightConstraint:NSLayoutConstraint?

    dynamic public var frameHeight:CGFloat{
        get{
            if let heightConstraint = heightConstraint{
                return heightConstraint.constant
            }
            else{
                return 50
            }
        }
        set{
            if let heightConstraint = heightConstraint{
                heightConstraint.constant = newValue
                layoutIfNeeded()

            }
            else{
                heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: newValue)
                heightConstraint?.priority = 999
                heightConstraint?.isActive = true
                layoutIfNeeded()

            }
        }
    }
    

So in your Themes/ThemeLabel, you can: YourLabel.appearance().frameHeight = 50

backbone's People

Contributors

bellots avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.