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:
- Without API Client
- With Alamofire
- 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.
I want to have a label which has red textColor and a blue backgroundColor.
-
Create a Swift file (if doesn't already exist) in Subclasses directory called UILabels.
-
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. -
Inside that file, add a class:
@IBDesignable class RedOnBlueLabel:DefaultLabel{}
-
Check if exists a file in Themes directory named ThemeLabels.swift or something like that. If not, create that
-
Add a struct called as you want (I suggest something like ThemeLabel
-
Add a function called
static func setStyle(for theme:Theme
) -
The
enum Theme
is an enum which you define in file Themes/Theme.swift -
If your app could be used in different modes (like
standard
anddark
), 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
}
}
- In Themes/Theme.swift, would be:
enum Theme{
case standard
case dark
func setStyle(){
ThemeLabel.setStyle(for:self)
}
}
- That's it! This
setStyle()
is called inAppDelegate->DidFinishLaunchingWithOptions
function.
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