Commit f47aaf8d by Orkhan Alikhanov Committed by OrkhanAlikhanov

Using swift 3.2 way to observe changes

parent 4def686b
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
import UIKit import UIKit
fileprivate var ToolbarContext: UInt8 = 0
open class Toolbar: Bar { open class Toolbar: Bar {
/// A convenience property to set the titleLabel.text. /// A convenience property to set the titleLabel.text.
@IBInspectable @IBInspectable
...@@ -65,10 +63,6 @@ open class Toolbar: Bar { ...@@ -65,10 +63,6 @@ open class Toolbar: Bar {
@IBInspectable @IBInspectable
public let detailLabel = UILabel() public let detailLabel = UILabel()
deinit {
removeObserver(self, forKeyPath: #keyPath(titleLabel.textAlignment))
}
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
...@@ -87,15 +81,6 @@ open class Toolbar: Bar { ...@@ -87,15 +81,6 @@ open class Toolbar: Bar {
super.init(frame: frame) super.init(frame: frame)
} }
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard "titleLabel.textAlignment" == keyPath else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
contentViewAlignment = .center == titleLabel.textAlignment ? .center : .full
}
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
guard willLayout else { guard willLayout else {
...@@ -143,16 +128,20 @@ open class Toolbar: Bar { ...@@ -143,16 +128,20 @@ open class Toolbar: Bar {
prepareTitleLabel() prepareTitleLabel()
prepareDetailLabel() prepareDetailLabel()
} }
private var titleLabelTextAlignmentObserver: NSKeyValueObservation!
} }
fileprivate extension Toolbar { private extension Toolbar {
/// Prepares the titleLabel. /// Prepares the titleLabel.
func prepareTitleLabel() { func prepareTitleLabel() {
titleLabel.textAlignment = .center titleLabel.textAlignment = .center
titleLabel.contentScaleFactor = Screen.scale titleLabel.contentScaleFactor = Screen.scale
titleLabel.font = RobotoFont.medium(with: 17) titleLabel.font = RobotoFont.medium(with: 17)
titleLabel.textColor = Color.darkText.primary titleLabel.textColor = Color.darkText.primary
addObserver(self, forKeyPath: #keyPath(titleLabel.textAlignment), options: [], context: &ToolbarContext) titleLabelTextAlignmentObserver = titleLabel.observe(\.textAlignment) { [weak self] titleLabel, _ in
self?.contentViewAlignment = .center == titleLabel.textAlignment ? .center : .full
}
} }
/// Prepares the detailLabel. /// Prepares the detailLabel.
......
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