Commit f47aaf8d by Orkhan Alikhanov Committed by OrkhanAlikhanov

Using swift 3.2 way to observe changes

parent 4def686b
......@@ -30,8 +30,6 @@
import UIKit
fileprivate var ToolbarContext: UInt8 = 0
open class Toolbar: Bar {
/// A convenience property to set the titleLabel.text.
@IBInspectable
......@@ -65,10 +63,6 @@ open class Toolbar: Bar {
@IBInspectable
public let detailLabel = UILabel()
deinit {
removeObserver(self, forKeyPath: #keyPath(titleLabel.textAlignment))
}
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
......@@ -87,15 +81,6 @@ open class Toolbar: Bar {
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() {
super.layoutSubviews()
guard willLayout else {
......@@ -143,16 +128,20 @@ open class Toolbar: Bar {
prepareTitleLabel()
prepareDetailLabel()
}
private var titleLabelTextAlignmentObserver: NSKeyValueObservation!
}
fileprivate extension Toolbar {
private extension Toolbar {
/// Prepares the titleLabel.
func prepareTitleLabel() {
titleLabel.textAlignment = .center
titleLabel.contentScaleFactor = Screen.scale
titleLabel.font = RobotoFont.medium(with: 17)
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.
......
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