Commit 79fcf1c3 by Daniel Dahan

development: fixed issue where title and detail were not centered when using…

development: fixed issue where title and detail were not centered when using left and right controls (issue-344)
parent f1d6b1d1
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
import UIKit import UIKit
import AVFoundation import AVFoundation
private var CaptureSessionAdjustingExposureContext: UInt8 = 1 private var CaptureSessionAdjustingExposureContext: UInt8 = 0
@objc(CaptureSessionPreset) @objc(CaptureSessionPreset)
public enum CaptureSessionPreset: Int { public enum CaptureSessionPreset: Int {
......
...@@ -31,9 +31,16 @@ ...@@ -31,9 +31,16 @@
import UIKit import UIKit
open class ControlView: View { open class ControlView: View {
/// Should center the contentView.
open var isCenteredContentView = false {
didSet {
layoutSubviews()
}
}
/// Will render the view. /// Will render the view.
open var willRenderView: Bool { open var willRenderView: Bool {
return 0 < width && 0 < height return 0 < width && 0 < height && nil != superview
} }
/// A preset wrapper around contentInset. /// A preset wrapper around contentInset.
...@@ -154,13 +161,14 @@ open class ControlView: View { ...@@ -154,13 +161,14 @@ open class ControlView: View {
let l = (CGFloat(leftControls.count) * interimSpace) let l = (CGFloat(leftControls.count) * interimSpace)
let r = (CGFloat(rightControls.count) * interimSpace) let r = (CGFloat(rightControls.count) * interimSpace)
let p = width - l - r - contentEdgeInsets.left - contentEdgeInsets.right let p = width - l - r - contentEdgeInsets.left - contentEdgeInsets.right
let columns = Int(p / gridFactor) var lc = 0
var rc = 0
let columns = Int(p / gridFactor)
grid.deferred = true
grid.views.removeAll() grid.views.removeAll()
grid.axis.columns = columns grid.axis.columns = columns
contentView.grid.columns = columns
for v in leftControls { for v in leftControls {
var w: CGFloat = 0 var w: CGFloat = 0
if let b = v as? UIButton { if let b = v as? UIButton {
...@@ -171,7 +179,7 @@ open class ControlView: View { ...@@ -171,7 +179,7 @@ open class ControlView: View {
v.height = frame.size.height - contentEdgeInsets.top - contentEdgeInsets.bottom v.height = frame.size.height - contentEdgeInsets.top - contentEdgeInsets.bottom
v.grid.columns = Int(ceil(w / gridFactor)) + 1 v.grid.columns = Int(ceil(w / gridFactor)) + 1
contentView.grid.columns -= v.grid.columns lc += v.grid.columns
grid.views.append(v) grid.views.append(v)
} }
...@@ -188,12 +196,15 @@ open class ControlView: View { ...@@ -188,12 +196,15 @@ open class ControlView: View {
v.height = frame.size.height - contentEdgeInsets.top - contentEdgeInsets.bottom v.height = frame.size.height - contentEdgeInsets.top - contentEdgeInsets.bottom
v.grid.columns = Int(ceil(w / gridFactor)) + 1 v.grid.columns = Int(ceil(w / gridFactor)) + 1
contentView.grid.columns -= v.grid.columns rc += v.grid.columns
grid.views.append(v) grid.views.append(v)
} }
contentView.grid.reload() contentView.grid.columns = columns - (isCenteredContentView ? 2 * max(lc, rc) : lc + rc)
grid.deferred = false
grid.reload()
} }
} }
......
...@@ -107,6 +107,9 @@ public class GridOffset { ...@@ -107,6 +107,9 @@ public class GridOffset {
} }
public class Grid { public class Grid {
/// Defer the calculation.
public var deferred = false
/// Context view. /// Context view.
internal weak var context: UIView? internal weak var context: UIView?
...@@ -196,6 +199,10 @@ public class Grid { ...@@ -196,6 +199,10 @@ public class Grid {
/// Reload the button layout. /// Reload the button layout.
public func reload() { public func reload() {
guard !deferred else {
return
}
var n: Int = 0 var n: Int = 0
var i: Int = 0 var i: Int = 0
......
...@@ -61,7 +61,7 @@ open class NavigationBar: UINavigationBar { ...@@ -61,7 +61,7 @@ open class NavigationBar: UINavigationBar {
/// Will render the view. /// Will render the view.
open var willRenderView: Bool { open var willRenderView: Bool {
return 0 < width && 0 < height return 0 < width && 0 < height && nil != superview
} }
/// A preset wrapper around contentInset. /// A preset wrapper around contentInset.
......
...@@ -33,7 +33,7 @@ import UIKit ...@@ -33,7 +33,7 @@ import UIKit
@objc(SnackbarStatus) @objc(SnackbarStatus)
public enum SnackbarStatus: Int { public enum SnackbarStatus: Int {
case visible case visible
case notVisible case hidden
} }
open class Snackbar: BarView { open class Snackbar: BarView {
...@@ -56,7 +56,7 @@ open class Snackbar: BarView { ...@@ -56,7 +56,7 @@ open class Snackbar: BarView {
} }
/// The status of the snackbar. /// The status of the snackbar.
open internal(set) var status = SnackbarStatus.notVisible open internal(set) var status = SnackbarStatus.hidden
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
/** /**
......
...@@ -30,76 +30,42 @@ ...@@ -30,76 +30,42 @@
import UIKit import UIKit
private var ToolbarContext: UInt8 = 0
open class Toolbar: BarView { open class Toolbar: BarView {
/// A convenience property to set the titleLabel text. /// A convenience property to set the titleLabel text.
open var title: String? { open var title: String? {
get { get {
return titleLabel?.text return titleLabel.text
} }
set(value) { set(value) {
titleLabel?.text = value titleLabel.text = value
layoutSubviews() layoutSubviews()
} }
} }
/// Title label. /// Title label.
open internal(set) var titleLabel: UILabel! open internal(set) var titleLabel = UILabel()
/// A convenience property to set the detailLabel text. /// A convenience property to set the detailLabel text.
open var detail: String? { open var detail: String? {
get { get {
return detailLabel?.text return detailLabel.text
} }
set(value) { set(value) {
detailLabel?.text = value detailLabel.text = value
layoutSubviews() layoutSubviews()
} }
} }
/// Detail label. /// Detail label.
open internal(set) var detailLabel: UILabel! open internal(set) var detailLabel = UILabel()
open override func layoutSubviews() {
super.layoutSubviews()
if willRenderView {
if nil != title && "" != title {
if nil == titleLabel.superview {
contentView.addSubview(titleLabel)
}
titleLabel.frame = contentView.bounds
} else {
titleLabel.removeFromSuperview()
}
if nil != detail && "" != detail {
if nil == detailLabel.superview {
contentView.addSubview(detailLabel)
}
if nil == titleLabel.superview {
detailLabel.frame = contentView.bounds
} else {
titleLabel.sizeToFit()
detailLabel.sizeToFit()
let diff: CGFloat = (contentView.frame.height - titleLabel.frame.height - detailLabel.frame.height) / 2
titleLabel.frame.size.height += diff
titleLabel.frame.size.width = contentView.frame.width
detailLabel.frame.size.height += diff
detailLabel.frame.size.width = contentView.frame.width
detailLabel.frame.origin.y = titleLabel.frame.height
}
} else {
detailLabel.removeFromSuperview()
}
contentView.grid.reload()
}
}
deinit {
removeObserver(self, forKeyPath: "titleLabel.textAlignment")
removeObserver(self, forKeyPath: "detailLabel.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.
...@@ -127,6 +93,50 @@ open class Toolbar: BarView { ...@@ -127,6 +93,50 @@ open class Toolbar: BarView {
super.init(leftControls: leftControls, rightControls: rightControls) super.init(leftControls: leftControls, rightControls: rightControls)
} }
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
isCenteredContentView = .center == titleLabel.textAlignment || .center == detailLabel.textAlignment
}
open override func layoutSubviews() {
super.layoutSubviews()
if willRenderView {
if nil != title && "" != title {
if nil == titleLabel.superview {
contentView.addSubview(titleLabel)
}
titleLabel.frame = contentView.bounds
} else {
titleLabel.removeFromSuperview()
}
if nil != detail && "" != detail {
if nil == detailLabel.superview {
contentView.addSubview(detailLabel)
}
if nil == titleLabel.superview {
detailLabel.frame = contentView.bounds
} else {
titleLabel.sizeToFit()
detailLabel.sizeToFit()
let diff: CGFloat = (contentView.height - titleLabel.height - detailLabel.height) / 2
titleLabel.height += diff
titleLabel.width = contentView.width
detailLabel.height += diff
detailLabel.width = contentView.width
detailLabel.y = titleLabel.height
}
} else {
detailLabel.removeFromSuperview()
}
contentView.grid.reload()
}
}
/** /**
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
...@@ -142,17 +152,15 @@ open class Toolbar: BarView { ...@@ -142,17 +152,15 @@ open class Toolbar: BarView {
/// Prepares the titleLabel. /// Prepares the titleLabel.
private func prepareTitleLabel() { private func prepareTitleLabel() {
titleLabel = UILabel()
titleLabel.contentScaleFactor = Device.scale titleLabel.contentScaleFactor = Device.scale
titleLabel.font = RobotoFont.medium(with: 17) titleLabel.font = RobotoFont.medium(with: 17)
titleLabel.textAlignment = .left addObserver(self, forKeyPath: "titleLabel.textAlignment", options: [], context: &ToolbarContext)
} }
/// Prepares the detailLabel. /// Prepares the detailLabel.
private func prepareDetailLabel() { private func prepareDetailLabel() {
detailLabel = UILabel()
detailLabel.contentScaleFactor = Device.scale detailLabel.contentScaleFactor = Device.scale
detailLabel.font = RobotoFont.regular(with: 12) detailLabel.font = RobotoFont.regular(with: 12)
detailLabel.textAlignment = .left addObserver(self, forKeyPath: "detailLabel.textAlignment", options: [], context: &ToolbarContext)
} }
} }
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