session-ios/SignalUtilitiesKit/Shared Views/OWSFlatButton.swift

192 lines
5.8 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
public class OWSFlatButton: UIView {
2019-08-23 07:12:08 +02:00
public let button: UIButton
private var pressedBlock : (() -> Void)?
private var upColor: UIColor?
private var downColor: UIColor?
@objc
public override var accessibilityIdentifier: String? {
didSet {
guard let accessibilityIdentifier = self.accessibilityIdentifier else {
return
}
button.accessibilityIdentifier = "\(accessibilityIdentifier).button"
}
}
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
}
}
@objc
public init() {
AssertIsOnMainThread()
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)
createContent()
}
@available(*, unavailable, message:"use other constructor instead.")
required public init?(coder aDecoder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
private func createContent() {
self.addSubview(button)
2018-04-11 21:17:34 +02:00
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
button.ows_autoPinToSuperviewEdges()
}
@objc
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,
selector: Selector) -> OWSFlatButton {
let button = OWSFlatButton()
2018-04-11 21:17:34 +02:00
button.setTitle(title: title,
font: font,
titleColor: titleColor )
2018-04-11 21:17:34 +02:00
button.setBackgroundColors(upColor: backgroundColor)
button.useDefaultCornerRadius()
2018-04-11 21:17:34 +02:00
button.setSize(width: width, height: height)
button.addTarget(target: target, selector: selector)
return button
}
@objc
public class func button(title: String,
titleColor: UIColor,
backgroundColor: UIColor,
width: CGFloat,
height: CGFloat,
2018-01-25 19:53:39 +01:00
target: Any,
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)
}
@objc
public class func button(title: String,
font: UIFont,
titleColor: UIColor,
backgroundColor: UIColor,
2018-01-25 19:53:39 +01:00
target: Any,
selector: Selector) -> OWSFlatButton {
let button = OWSFlatButton()
2018-04-11 21:17:34 +02:00
button.setTitle(title: title,
font: font,
titleColor: titleColor )
2018-04-11 21:17:34 +02:00
button.setBackgroundColors(upColor: backgroundColor)
button.useDefaultCornerRadius()
2018-04-11 21:17:34 +02:00
button.addTarget(target: target, selector: selector)
return button
}
@objc
public class func fontForHeight(_ height: CGFloat) -> UIFont {
// 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)
}
// MARK: Methods
2019-04-30 01:34:14 +02:00
@objc
public func setTitle(_ title: String) {
button.setTitle(title, for: .normal)
}
@objc
public func setTitle(title: String, font: UIFont,
titleColor: UIColor ) {
button.setTitle(title, for: .normal)
button.setTitleColor(titleColor, for: .normal)
button.titleLabel!.font = font
}
@objc
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)
}
@objc
2017-09-18 21:47:59 +02:00
public func setBackgroundColors(upColor: UIColor ) {
2020-08-26 04:02:27 +02:00
setBackgroundColors(upColor: upColor, downColor: upColor )
}
@objc
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)
}
@objc
public func useDefaultCornerRadius() {
// To my eye, this radius tends to look right regardless of button size
// (within reason) or device size.
button.layer.cornerRadius = 5
button.clipsToBounds = true
}
@objc
public func setEnabled(_ isEnabled: Bool) {
button.isEnabled = isEnabled
}
@objc
2018-01-25 19:53:39 +01:00
public func addTarget(target: Any,
selector: Selector) {
2018-04-11 21:17:34 +02:00
button.addTarget(target, action: selector, for: .touchUpInside)
}
@objc
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.")
return
}
self.pressedBlock = pressedBlock
}
@objc
internal func buttonPressed() {
2017-09-18 21:47:59 +02:00
pressedBlock?()
}
@objc
public func enableMultilineLabel() {
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
}
}