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.
		
		
		
		
		
			
		
			
	
	
		
			192 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			192 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			Swift
		
	
| 
								 
											8 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								//  Copyright (c) 2019 Open Whisper Systems. All rights reserved.
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import Foundation
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@objc
							 | 
						||
| 
								 | 
							
								public class OWSFlatButton: UIView {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public let button: UIButton
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    private var pressedBlock : (() -> Void)?
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    private var upColor: UIColor?
							 | 
						||
| 
								 | 
							
								    private var downColor: UIColor?
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public override var accessibilityIdentifier: String? {
							 | 
						||
| 
								 | 
							
								        didSet {
							 | 
						||
| 
								 | 
							
								            guard let accessibilityIdentifier = self.accessibilityIdentifier else {
							 | 
						||
| 
								 | 
							
								                return
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            button.accessibilityIdentifier = "\(accessibilityIdentifier).button"
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    override public var backgroundColor: UIColor? {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        willSet {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								            owsFailDebug("Use setBackgroundColors(upColor:) instead.")
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public init() {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        AssertIsOnMainThread()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button = UIButton(type: .custom)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        super.init(frame: CGRect.zero)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								        createContent()
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @available(*, unavailable, message:"use other constructor instead.")
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    required public init?(coder aDecoder: NSCoder) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        notImplemented()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    private func createContent() {
							 | 
						||
| 
								 | 
							
								        self.addSubview(button)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        button.ows_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,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             target: Any,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             selector: Selector) -> OWSFlatButton {
							 | 
						||
| 
								 | 
							
								        let button = OWSFlatButton()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setTitle(title: title,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                        font: font,
							 | 
						||
| 
								 | 
							
								                        titleColor: titleColor )
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setBackgroundColors(upColor: backgroundColor)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.useDefaultCornerRadius()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setSize(width: width, height: height)
							 | 
						||
| 
								 | 
							
								        button.addTarget(target: target, selector: selector)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        return button
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    public class func button(title: String,
							 | 
						||
| 
								 | 
							
								                             titleColor: UIColor,
							 | 
						||
| 
								 | 
							
								                             backgroundColor: UIColor,
							 | 
						||
| 
								 | 
							
								                             width: CGFloat,
							 | 
						||
| 
								 | 
							
								                             height: CGFloat,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             target: Any,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             selector: Selector) -> OWSFlatButton {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        return OWSFlatButton.button(title: title,
							 | 
						||
| 
								 | 
							
								                                    font: fontForHeight(height),
							 | 
						||
| 
								 | 
							
								                                    titleColor: titleColor,
							 | 
						||
| 
								 | 
							
								                                    backgroundColor: backgroundColor,
							 | 
						||
| 
								 | 
							
								                                    width: width,
							 | 
						||
| 
								 | 
							
								                                    height: height,
							 | 
						||
| 
								 | 
							
								                                    target: target,
							 | 
						||
| 
								 | 
							
								                                    selector: selector)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    public class func button(title: String,
							 | 
						||
| 
								 | 
							
								                             font: UIFont,
							 | 
						||
| 
								 | 
							
								                             titleColor: UIColor,
							 | 
						||
| 
								 | 
							
								                             backgroundColor: UIColor,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             target: Any,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                             selector: Selector) -> OWSFlatButton {
							 | 
						||
| 
								 | 
							
								        let button = OWSFlatButton()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setTitle(title: title,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                        font: font,
							 | 
						||
| 
								 | 
							
								                        titleColor: titleColor )
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setBackgroundColors(upColor: backgroundColor)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.useDefaultCornerRadius()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.addTarget(target: target, selector: selector)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        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)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // MARK: Methods
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public func setTitle(_ title: String) {
							 | 
						||
| 
								 | 
							
								        button.setTitle(title, for: .normal)
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 
											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 ) {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.setBackgroundImage(UIImage(color: upColor), for: .normal)
							 | 
						||
| 
								 | 
							
								        button.setBackgroundImage(UIImage(color: downColor), for: .highlighted)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    public func setBackgroundColors(upColor: UIColor ) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        setBackgroundColors(upColor: upColor, downColor: upColor )
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    public func setSize(width: CGFloat, height: CGFloat) {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.autoSetDimension(.width, toSize: width)
							 | 
						||
| 
								 | 
							
								        button.autoSetDimension(.height, toSize: height)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											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,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                          selector: Selector) {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        button.addTarget(target, action: selector, for: .touchUpInside)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    public func setPressedBlock(_ pressedBlock: @escaping () -> Void) {
							 | 
						||
| 
								 | 
							
								        guard self.pressedBlock == nil else {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								            owsFailDebug("Button already has pressed block.")
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								            return
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        self.pressedBlock = pressedBlock
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    @objc
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    internal func buttonPressed() {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        pressedBlock?()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public func enableMultilineLabel() {
							 | 
						||
| 
								 | 
							
								        button.titleLabel?.numberOfLines = 0
							 | 
						||
| 
								 | 
							
								        button.titleLabel?.lineBreakMode = .byWordWrapping
							 | 
						||
| 
								 | 
							
								        button.titleLabel?.textAlignment = .center
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 |