2017-09-12 16:14:17 +02:00
|
|
|
//
|
2019-02-15 20:19:12 +01:00
|
|
|
// Copyright (c) 2019 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
|
|
|
|
2019-08-23 07:12:08 +02:00
|
|
|
public let button: UIButton
|
2017-09-12 16:14:17 +02:00
|
|
|
|
|
|
|
private var pressedBlock : (() -> Void)?
|
|
|
|
|
|
|
|
private var upColor: UIColor?
|
|
|
|
private var downColor: UIColor?
|
|
|
|
|
2019-03-19 20:43:35 +01:00
|
|
|
@objc
|
|
|
|
public override var accessibilityIdentifier: String? {
|
|
|
|
didSet {
|
|
|
|
guard let accessibilityIdentifier = self.accessibilityIdentifier else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
button.accessibilityIdentifier = "\(accessibilityIdentifier).button"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
override public var backgroundColor: UIColor? {
|
2017-09-18 21:47:59 +02:00
|
|
|
willSet {
|
2018-08-27 16:27:48 +02:00
|
|
|
owsFailDebug("Use setBackgroundColors(upColor:) instead.")
|
2017-09-18 21:47:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
@objc
|
|
|
|
public init() {
|
2018-08-22 19:44:22 +02:00
|
|
|
AssertIsOnMainThread()
|
2017-09-12 16:14:17 +02:00
|
|
|
|
2018-04-11 21:17:34 +02:00
|
|
|
button = UIButton(type: .custom)
|
2017-09-18 21:47:59 +02:00
|
|
|
|
2018-04-11 21:17:34 +02:00
|
|
|
super.init(frame: CGRect.zero)
|
2017-09-12 16:14:17 +02:00
|
|
|
|
|
|
|
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) {
|
2018-08-27 16:21:03 +02:00
|
|
|
notImplemented()
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func createContent() {
|
|
|
|
self.addSubview(button)
|
2018-04-11 21:17:34 +02:00
|
|
|
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
|
2018-07-14 00:26:58 +02:00
|
|
|
button.ows_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()
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setTitle(title: title,
|
2017-09-12 16:14:17 +02:00
|
|
|
font: font,
|
|
|
|
titleColor: titleColor )
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setBackgroundColors(upColor: backgroundColor)
|
2017-09-12 16:14:17 +02:00
|
|
|
button.useDefaultCornerRadius()
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setSize(width: width, height: height)
|
|
|
|
button.addTarget(target: target, selector: selector)
|
2017-09-12 16:14:17 +02:00
|
|
|
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 {
|
2018-04-11 21:17:34 +02:00
|
|
|
return OWSFlatButton.button(title: title,
|
|
|
|
font: fontForHeight(height),
|
|
|
|
titleColor: titleColor,
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
target: target,
|
|
|
|
selector: selector)
|
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,
|
2018-01-25 19:53:39 +01:00
|
|
|
target: Any,
|
2017-09-12 16:14:17 +02:00
|
|
|
selector: Selector) -> OWSFlatButton {
|
|
|
|
let button = OWSFlatButton()
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setTitle(title: title,
|
2017-09-12 16:14:17 +02:00
|
|
|
font: font,
|
|
|
|
titleColor: titleColor )
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setBackgroundColors(upColor: backgroundColor)
|
2017-09-12 16:14:17 +02:00
|
|
|
button.useDefaultCornerRadius()
|
2018-04-11 21:17:34 +02:00
|
|
|
button.addTarget(target: target, selector: selector)
|
2017-09-12 16:14:17 +02:00
|
|
|
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-04-11 21:17:34 +02:00
|
|
|
return UIFont.ows_mediumFont(withSize: fontPointSize)
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
2019-04-30 01:34:14 +02:00
|
|
|
@objc
|
|
|
|
public func setTitle(_ title: String) {
|
|
|
|
button.setTitle(title, for: .normal)
|
|
|
|
}
|
|
|
|
|
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 ) {
|
2018-04-11 21:17:34 +02:00
|
|
|
button.setBackgroundImage(UIImage(color: upColor), for: .normal)
|
|
|
|
button.setBackgroundImage(UIImage(color: downColor), for: .highlighted)
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-04-11 21:17:34 +02:00
|
|
|
button.autoSetDimension(.width, toSize: width)
|
|
|
|
button.autoSetDimension(.height, toSize: height)
|
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 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) {
|
2018-04-11 21:17:34 +02:00
|
|
|
button.addTarget(target, action: selector, for: .touchUpInside)
|
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 setPressedBlock(_ pressedBlock: @escaping () -> Void) {
|
|
|
|
guard self.pressedBlock == nil else {
|
2018-08-27 16:27:48 +02:00
|
|
|
owsFailDebug("Button already has pressed block.")
|
2017-09-12 16:14:17 +02:00
|
|
|
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
|
|
|
}
|
2019-02-15 20:19:12 +01:00
|
|
|
|
|
|
|
@objc
|
|
|
|
public func enableMultilineLabel() {
|
|
|
|
button.titleLabel?.numberOfLines = 0
|
|
|
|
button.titleLabel?.lineBreakMode = .byWordWrapping
|
|
|
|
button.titleLabel?.textAlignment = .center
|
|
|
|
}
|
2017-09-12 16:14:17 +02:00
|
|
|
}
|