mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			172 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			172 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Swift
		
	
| 
											8 years ago
										 | // | ||
|  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | import Foundation | ||
| 
											8 years ago
										 | import SignalServiceKit | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 | @objc | ||
|  | public class OWSFlatButton: UIView { | ||
| 
											8 years ago
										 |     let TAG = "[OWSFlatButton]" | ||
|  | 
 | ||
| 
											8 years ago
										 |     private let button: UIButton | ||
| 
											8 years ago
										 | 
 | ||
|  |     private var pressedBlock : (() -> Void)? | ||
|  | 
 | ||
|  |     private var upColor: UIColor? | ||
|  |     private var downColor: UIColor? | ||
|  | 
 | ||
| 
											8 years ago
										 |     override public var backgroundColor: UIColor? { | ||
| 
											8 years ago
										 |         willSet { | ||
|  |             owsFail("Use setBackgroundColors(upColor:) instead.") | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public init() { | ||
| 
											8 years ago
										 |         AssertIsOnMainThread() | ||
|  | 
 | ||
| 
											8 years ago
										 |         button = UIButton(type:.custom) | ||
|  | 
 | ||
| 
											8 years ago
										 |         super.init(frame:CGRect.zero) | ||
|  | 
 | ||
|  |         createContent() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @available(*, unavailable, message:"use other constructor instead.") | ||
| 
											8 years ago
										 |     required public init?(coder aDecoder: NSCoder) { | ||
| 
											8 years ago
										 |         fatalError("\(#function) is unimplemented.") | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
|  |     private func createContent() { | ||
|  |         self.addSubview(button) | ||
|  |         button.addTarget(self, action:#selector(buttonPressed), for:.touchUpInside) | ||
| 
											8 years ago
										 |         button.autoPinToSuperviewEdges() | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public class func button(title: String, | ||
|  |                              font: UIFont, | ||
|  |                              titleColor: UIColor, | ||
|  |                              backgroundColor: UIColor, | ||
|  |                              width: CGFloat, | ||
|  |                              height: CGFloat, | ||
|  |                              target:Any, | ||
|  |                              selector: Selector) -> OWSFlatButton { | ||
|  |         let button = OWSFlatButton() | ||
|  |         button.setTitle(title:title, | ||
|  |                         font: font, | ||
|  |                         titleColor: titleColor ) | ||
| 
											8 years ago
										 |         button.setBackgroundColors(upColor:backgroundColor) | ||
| 
											8 years ago
										 |         button.useDefaultCornerRadius() | ||
|  |         button.setSize(width:width, height:height) | ||
|  |         button.addTarget(target:target, selector:selector) | ||
|  |         return button | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public class func button(title: String, | ||
|  |                              titleColor: UIColor, | ||
|  |                              backgroundColor: UIColor, | ||
|  |                              width: CGFloat, | ||
|  |                              height: CGFloat, | ||
|  |                              target:Any, | ||
|  |                              selector: Selector) -> OWSFlatButton { | ||
|  |         return OWSFlatButton.button(title:title, | ||
|  |                                     font:fontForHeight(height), | ||
|  |                                     titleColor:titleColor, | ||
|  |                                     backgroundColor:backgroundColor, | ||
|  |                                     width:width, | ||
|  |                                     height:height, | ||
|  |                                     target:target, | ||
|  |                                     selector:selector) | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public class func button(title: String, | ||
|  |                              font: UIFont, | ||
|  |                              titleColor: UIColor, | ||
|  |                              backgroundColor: UIColor, | ||
|  |                              target:Any, | ||
|  |                              selector: Selector) -> OWSFlatButton { | ||
|  |         let button = OWSFlatButton() | ||
|  |         button.setTitle(title:title, | ||
|  |                         font: font, | ||
|  |                         titleColor: titleColor ) | ||
| 
											8 years ago
										 |         button.setBackgroundColors(upColor:backgroundColor) | ||
| 
											8 years ago
										 |         button.useDefaultCornerRadius() | ||
|  |         button.addTarget(target:target, selector:selector) | ||
|  |         return button | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public class func fontForHeight(_ height: CGFloat) -> UIFont { | ||
| 
											8 years ago
										 |         // Cap the "button height" at 40pt or button text can look | ||
|  |         // excessively large. | ||
|  |         let fontPointSize = round(min(40, height) * 0.45) | ||
| 
											8 years ago
										 |         return UIFont.ows_mediumFont(withSize:fontPointSize)! | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: Methods | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setTitle(title: String, font: UIFont, | ||
|  |                          titleColor: UIColor ) { | ||
|  |         button.setTitle(title, for: .normal) | ||
|  |         button.setTitleColor(titleColor, for: .normal) | ||
|  |         button.titleLabel!.font = font | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setBackgroundColors(upColor: UIColor, | ||
|  |                                     downColor: UIColor ) { | ||
|  |         button.setBackgroundImage(UIImage(color:upColor), for: .normal) | ||
|  |         button.setBackgroundImage(UIImage(color:downColor), for: .highlighted) | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setBackgroundColors(upColor: UIColor ) { | ||
|  |         setBackgroundColors(upColor: upColor, | ||
|  |                             downColor: upColor.withAlphaComponent(0.7) ) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setSize(width: CGFloat, height: CGFloat) { | ||
|  |         button.autoSetDimension(.width, toSize:width) | ||
|  |         button.autoSetDimension(.height, toSize:height) | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func useDefaultCornerRadius() { | ||
| 
											8 years ago
										 |         // To my eye, this radius tends to look right regardless of button size | ||
|  |         // (within reason) or device size.  | ||
|  |         button.layer.cornerRadius = 5 | ||
| 
											8 years ago
										 |         button.clipsToBounds = true | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setEnabled(_ isEnabled: Bool) { | ||
|  |         button.isEnabled = isEnabled | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func addTarget(target:Any, | ||
|  |                           selector: Selector) { | ||
|  |         button.addTarget(target, action:selector, for:.touchUpInside) | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func setPressedBlock(_ pressedBlock: @escaping () -> Void) { | ||
|  |         guard self.pressedBlock == nil else { | ||
|  |             owsFail("Button already has pressed block.") | ||
|  |             return | ||
|  |         } | ||
|  |         self.pressedBlock = pressedBlock | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     internal func buttonPressed() { | ||
| 
											8 years ago
										 |         pressedBlock?() | ||
| 
											8 years ago
										 |     } | ||
|  | } |