Commit 091d4306 by Daniel Jonathan Committed by GitHub

Merge pull request #1183 from OrkhanAlikhanov/global-font

Global font and navigationItem.backButton issue
parents 0b1d7707 0cd62140
......@@ -279,6 +279,7 @@ open class Button: UIButton, Pulseable, PulseableLayer, Themeable {
*/
open func prepare() {
contentScaleFactor = Screen.scale
titleLabel?.font = Theme.font.regular(with: fontSize)
prepareVisualLayer()
preparePulse()
applyCurrentTheme()
......
......@@ -233,7 +233,7 @@ private extension DialogView {
/// Prepares titleTitle.
func prepareTitleLabel() {
titleArea.addSubview(titleLabel)
titleLabel.font = RobotoFont.bold(with: 20)
titleLabel.font = Theme.font.bold(with: 20)
titleLabel.textColor = Color.darkText.primary
titleLabel.numberOfLines = 0
}
......@@ -250,7 +250,7 @@ private extension DialogView {
func prepareButtons() {
[positiveButton, negativeButton, neutralButton].forEach {
buttonArea.addSubview($0)
$0.titleLabel?.font = RobotoFont.medium(with: 14)
$0.titleLabel?.font = Theme.font.medium(with: 14)
$0.contentEdgeInsets = Constants.button.insets
$0.cornerRadiusPreset = .cornerRadius1
}
......@@ -270,6 +270,7 @@ private extension DialogView {
/// Prepares detailsLabel.
func prepareDetailsLabel() {
contentArea.addSubview(detailsLabel)
detailsLabel.font = Theme.font.regular(with: detailsLabel.fontSize)
detailsLabel.numberOfLines = 0
detailsLabel.textColor = Color.darkText.secondary
}
......
......@@ -251,7 +251,7 @@ private extension Editor {
/// Prepares the detailLabel.
func prepareDetailLabel() {
detailLabel.font = RobotoFont.regular(with: 12)
detailLabel.font = Theme.font.regular(with: 12)
detailLabel.numberOfLines = 0
detailColor = Color.darkText.others
addSubview(detailLabel)
......
......@@ -83,7 +83,7 @@ open class ErrorTextField: TextField {
/// Prepares the errorLabel.
func prepareErrorLabel() {
errorLabel.font = RobotoFont.regular(with: 12)
errorLabel.font = Theme.font.regular(with: 12)
errorLabel.numberOfLines = 0
errorColor = { errorColor }() // call didSet
addSubview(errorLabel)
......
......@@ -129,7 +129,7 @@ extension FABMenuItem {
/// Prepares the titleLabel.
fileprivate func prepareTitleLabel() {
titleLabel.font = RobotoFont.regular(with: 14)
titleLabel.font = Theme.font.regular(with: 14)
titleLabel.textAlignment = .center
titleLabel.backgroundColor = .white
titleLabel.depthPreset = fabButton.depthPreset
......
......@@ -30,7 +30,28 @@
import UIKit
public protocol FontType {}
public protocol FontType {
/**
Regular with size font.
- Parameter with size: A CGFLoat for the font size.
- Returns: A UIFont.
*/
static func regular(with size: CGFloat) -> UIFont
/**
Medium with size font.
- Parameter with size: A CGFLoat for the font size.
- Returns: A UIFont.
*/
static func medium(with size: CGFloat) -> UIFont
/**
Bold with size font.
- Parameter with size: A CGFLoat for the font size.
- Returns: A UIFont.
*/
static func bold(with size: CGFloat) -> UIFont
}
public struct Font {
/// Size of font.
......
......@@ -146,6 +146,22 @@ open class NavigationController: UINavigationController {
navigationBar.setNeedsLayout()
navigationBar.layoutIfNeeded()
}
/**
Sets whether the navigation bar is hidden.
- Parameter _ hidden: Specify true to hide the navigation bar or false to show it.
- Parameter animated: Specify true if you want to animate the change in visibility or false if you want the navigation bar to appear immediately.
*/
open override func setNavigationBarHidden(_ hidden: Bool, animated: Bool) {
super.setNavigationBarHidden(hidden, animated: animated)
guard let items = navigationBar.items, items.count > 1 else {
return
}
items.forEach {
prepareBackButton(for: $0, in: navigationBar)
}
}
}
extension NavigationController: UINavigationBarDelegate {
......@@ -158,32 +174,12 @@ extension NavigationController: UINavigationBarDelegate {
True is yes, false is no.
*/
public func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
if let v = navigationBar as? NavigationBar {
if nil == item.backButton.image && nil == item.backButton.title {
item.backButton.image = v.backButtonImage
}
if !item.backButton.isHidden {
item.leftViews.insert(item.backButton, at: 0)
}
item.backButton.addTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
item.hidesBackButton = false
item.setHidesBackButton(true, animated: false)
v.layoutNavigationItem(item: item)
}
prepareBackButton(for: item, in: navigationBar)
return true
}
public func navigationBar(_ navigationBar: UINavigationBar, didPop item: UINavigationItem) {
if let index = item.leftViews.index(of: item.backButton) {
item.leftViews.remove(at: index)
}
item.backButton.removeTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
removeBackButton(from: item)
}
}
......@@ -193,6 +189,44 @@ internal extension NavigationController {
func handle(backButton: UIButton) {
popViewController(animated: true)
}
/**
Prepares back button of the navigation item in navigation bar.
- Parameter for item: A UINavigationItem.
- Parameter in navigationBar: A UINavigationBar.
*/
func prepareBackButton(for item: UINavigationItem, in navigationBar: UINavigationBar) {
guard let v = navigationBar as? NavigationBar else {
return
}
if nil == item.backButton.image && nil == item.backButton.title {
item.backButton.image = v.backButtonImage
}
if !item.backButton.isHidden && !item.leftViews.contains(item.backButton) {
item.leftViews.insert(item.backButton, at: 0)
}
item.backButton.addTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
item.hidesBackButton = false
item.setHidesBackButton(true, animated: false)
v.layoutNavigationItem(item: item)
}
/**
Removes back button of the navigation item.
- Parameter from item: A UINavigationItem.
*/
func removeBackButton(from item: UINavigationItem) {
if let index = item.leftViews.index(of: item.backButton) {
item.leftViews.remove(at: index)
}
item.backButton.removeTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
}
}
extension NavigationController: UIGestureRecognizerDelegate {
......
......@@ -213,7 +213,7 @@ fileprivate extension SearchBar {
/// Prepares the textField.
func prepareTextField() {
textField.contentScaleFactor = Screen.scale
textField.font = RobotoFont.regular(with: 17)
textField.font = Theme.font.regular(with: 17)
textField.backgroundColor = Color.clear
textField.clearButtonMode = .whileEditing
textField.addTarget(self, action: #selector(handleEditingChanged(textField:)), for: .editingChanged)
......
......@@ -104,7 +104,7 @@ open class Snackbar: Bar {
/// Prepares the textLabel.
private func prepareTextLabel() {
textLabel.contentScaleFactor = Screen.scale
textLabel.font = RobotoFont.medium(with: 14)
textLabel.font = Theme.font.medium(with: 14)
textLabel.textAlignment = .left
textLabel.textColor = .white
textLabel.numberOfLines = 0
......
......@@ -450,7 +450,7 @@ open class TextField: UITextField, Themeable {
borderStyle = .none
backgroundColor = nil
contentScaleFactor = Screen.scale
font = RobotoFont.regular(with: 16)
font = Theme.font.regular(with: 16)
textColor = Color.darkText.primary
prepareDivider()
......@@ -496,7 +496,7 @@ fileprivate extension TextField {
/// Prepares the detailLabel.
func prepareDetailLabel() {
detailLabel.font = RobotoFont.regular(with: 12)
detailLabel.font = Theme.font.regular(with: 12)
detailLabel.numberOfLines = 0
detailColor = Color.darkText.others
addSubview(detailLabel)
......
......@@ -283,7 +283,7 @@ open class TextView: UITextView, Themeable {
contentScaleFactor = Screen.scale
textContainerInset = .zero
backgroundColor = nil
font = RobotoFont.regular(with: 16)
font = Theme.font.regular(with: 16)
textColor = Color.darkText.primary
prepareNotificationHandlers()
......
......@@ -77,6 +77,9 @@ public struct Theme: Hashable {
/// A boolean indicating if theming is enabled globally.
public static var isEnabled = true
/// Global font for app.
public static var font: FontType.Type = RobotoFont.self
/// An initializer.
public init() { }
}
......
......@@ -184,7 +184,7 @@ private extension Toolbar {
func prepareTitleLabel() {
titleLabel.textAlignment = .center
titleLabel.contentScaleFactor = Screen.scale
titleLabel.font = RobotoFont.medium(with: 17)
titleLabel.font = Theme.font.medium(with: 17)
titleLabel.textColor = Color.darkText.primary
titleLabelTextAlignmentObserver = titleLabel.observe(\.textAlignment) { [weak self] titleLabel, _ in
self?.contentViewAlignment = .center == titleLabel.textAlignment ? .center : .full
......@@ -195,7 +195,7 @@ private extension Toolbar {
func prepareDetailLabel() {
detailLabel.textAlignment = .center
detailLabel.contentScaleFactor = Screen.scale
detailLabel.font = RobotoFont.regular(with: 12)
detailLabel.font = Theme.font.regular(with: 12)
detailLabel.textColor = Color.darkText.secondary
}
......
......@@ -45,6 +45,7 @@ open class ViewController: UIViewController, Themeable {
*/
open func prepare() {
view.clipsToBounds = true
view.backgroundColor = .white
view.contentScaleFactor = Screen.scale
applyCurrentTheme()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment