2017-09-12 16:14:17 +02:00
|
|
|
//
|
2018-01-25 19:53:39 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-09-12 16:14:17 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-11-28 00:17:46 +01:00
|
|
|
import SignalServiceKit
|
2017-09-12 16:14:17 +02:00
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
|
|
|
public class OWSFlatButton: UIView {
|
2017-09-12 16:14:17 +02:00
|
|
|
let TAG = "[OWSFlatButton]"
|
|
|
|
|
2017-09-18 21:47:59 +02:00
|
|
|
private let button: UIButton
|
2017-09-12 16:14:17 +02:00
|
|
|
|
|
|
|
private var pressedBlock : (() -> Void)?
|
|
|
|
|
|
|
|
private var upColor: UIColor?
|
|
|
|
private var downColor: UIColor?
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
override public var backgroundColor: UIColor? {
|
2017-09-18 21:47:59 +02:00
|
|
|
willSet {
|
|
|
|
owsFail("Use setBackgroundColors(upColor:) instead.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
|
|
|
public init() {
|
2017-09-12 16:14:17 +02:00
|
|
|
AssertIsOnMainThread()
|
|
|
|
|
2017-09-18 21:47:59 +02:00
|
|
|
button = UIButton(type:.custom)
|
|
|
|
|
2017-09-12 16:14:17 +02:00
|
|
|
super.init(frame:CGRect.zero)
|
|
|
|
|
|
|
|
createContent()
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:53:54 +02:00
|
|
|
@available(*, unavailable, message:"use other constructor instead.")
|
2017-12-04 23:17:44 +01:00
|
|
|
required public init?(coder aDecoder: NSCoder) {
|
2017-10-25 20:53:54 +02:00
|
|
|
fatalError("\(#function) is unimplemented.")
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func createContent() {
|
|
|
|
self.addSubview(button)
|
|
|
|
button.addTarget(self, action:#selector(buttonPressed), for:.touchUpInside)
|
2017-10-02 20:26:03 +02:00
|
|
|
button.autoPinToSuperviewEdges()
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public class func button(title: String,
|
|
|
|
font: UIFont,
|
|
|
|
titleColor: UIColor,
|
|
|
|
backgroundColor: UIColor,
|
|
|
|
width: CGFloat,
|
|
|
|
height: CGFloat,
|
2018-01-25 19:53:39 +01:00
|
|
|
target: Any,
|
2017-09-12 16:14:17 +02:00
|
|
|
selector: Selector) -> OWSFlatButton {
|
|
|
|
let button = OWSFlatButton()
|
|
|
|
button.setTitle(title:title,
|
|
|
|
font: font,
|
|
|
|
titleColor: titleColor )
|
2017-09-18 21:47:59 +02:00
|
|
|
button.setBackgroundColors(upColor:backgroundColor)
|
2017-09-12 16:14:17 +02:00
|
|
|
button.useDefaultCornerRadius()
|
|
|
|
button.setSize(width:width, height:height)
|
|
|
|
button.addTarget(target:target, selector:selector)
|
|
|
|
return button
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public class func button(title: String,
|
|
|
|
titleColor: UIColor,
|
|
|
|
backgroundColor: UIColor,
|
|
|
|
width: CGFloat,
|
|
|
|
height: CGFloat,
|
2018-01-25 19:53:39 +01:00
|
|
|
target: Any,
|
2017-09-12 16:14:17 +02:00
|
|
|
selector: Selector) -> OWSFlatButton {
|
|
|
|
return OWSFlatButton.button(title:title,
|
|
|
|
font:fontForHeight(height),
|
|
|
|
titleColor:titleColor,
|
|
|
|
backgroundColor:backgroundColor,
|
|
|
|
width:width,
|
|
|
|
height:height,
|
|
|
|
target:target,
|
|
|
|
selector:selector)
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public class func button(title: String,
|
|
|
|
font: UIFont,
|
|
|
|
titleColor: UIColor,
|
|
|
|
backgroundColor: UIColor,
|
2018-01-25 19:53:39 +01:00
|
|
|
target: Any,
|
2017-09-12 16:14:17 +02:00
|
|
|
selector: Selector) -> OWSFlatButton {
|
|
|
|
let button = OWSFlatButton()
|
|
|
|
button.setTitle(title:title,
|
|
|
|
font: font,
|
|
|
|
titleColor: titleColor )
|
2017-09-18 21:47:59 +02:00
|
|
|
button.setBackgroundColors(upColor:backgroundColor)
|
2017-09-12 16:14:17 +02:00
|
|
|
button.useDefaultCornerRadius()
|
|
|
|
button.addTarget(target:target, selector:selector)
|
|
|
|
return button
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public class func fontForHeight(_ height: CGFloat) -> UIFont {
|
2017-09-29 16:35:13 +02:00
|
|
|
// Cap the "button height" at 40pt or button text can look
|
|
|
|
// excessively large.
|
|
|
|
let fontPointSize = round(min(40, height) * 0.45)
|
2018-01-25 19:53:39 +01:00
|
|
|
return UIFont.ows_mediumFont(withSize:fontPointSize)
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func setTitle(title: String, font: UIFont,
|
|
|
|
titleColor: UIColor ) {
|
|
|
|
button.setTitle(title, for: .normal)
|
|
|
|
button.setTitleColor(titleColor, for: .normal)
|
|
|
|
button.titleLabel!.font = font
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func setBackgroundColors(upColor: UIColor,
|
|
|
|
downColor: UIColor ) {
|
|
|
|
button.setBackgroundImage(UIImage(color:upColor), for: .normal)
|
|
|
|
button.setBackgroundImage(UIImage(color:downColor), for: .highlighted)
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-18 21:47:59 +02:00
|
|
|
public func setBackgroundColors(upColor: UIColor ) {
|
|
|
|
setBackgroundColors(upColor: upColor,
|
|
|
|
downColor: upColor.withAlphaComponent(0.7) )
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func setSize(width: CGFloat, height: CGFloat) {
|
|
|
|
button.autoSetDimension(.width, toSize:width)
|
|
|
|
button.autoSetDimension(.height, toSize:height)
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func useDefaultCornerRadius() {
|
2017-09-12 16:27:22 +02:00
|
|
|
// To my eye, this radius tends to look right regardless of button size
|
|
|
|
// (within reason) or device size.
|
|
|
|
button.layer.cornerRadius = 5
|
2017-09-12 16:14:17 +02:00
|
|
|
button.clipsToBounds = true
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func setEnabled(_ isEnabled: Bool) {
|
|
|
|
button.isEnabled = isEnabled
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2018-01-25 19:53:39 +01:00
|
|
|
public func addTarget(target: Any,
|
2017-09-12 16:14:17 +02:00
|
|
|
selector: Selector) {
|
|
|
|
button.addTarget(target, action:selector, for:.touchUpInside)
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
public func setPressedBlock(_ pressedBlock: @escaping () -> Void) {
|
|
|
|
guard self.pressedBlock == nil else {
|
|
|
|
owsFail("Button already has pressed block.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.pressedBlock = pressedBlock
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
2017-09-12 16:14:17 +02:00
|
|
|
internal func buttonPressed() {
|
2017-09-18 21:47:59 +02:00
|
|
|
pressedBlock?()
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
}
|