74 lines
2.0 KiB
Swift
74 lines
2.0 KiB
Swift
//
|
|
// EQNRoundedButton.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 11/09/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class EQNRoundedButton: UIButton {
|
|
|
|
// MARK: - Init
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
setupUI()
|
|
}
|
|
|
|
// MARK: - Overrides
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
get {
|
|
return titleLabel?.intrinsicContentSize ?? CGSize.zero
|
|
}
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
// adapt button height to titleLabel size
|
|
titleLabel?.preferredMaxLayoutWidth = titleLabel?.frame.size.width ?? 0
|
|
super.layoutSubviews()
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func setupUI() {
|
|
// add border
|
|
layer.masksToBounds = true
|
|
layer.borderWidth = AppTheme.shared.buttonBorderWidth
|
|
layer.borderColor = AppTheme.Colors.gray.cgColor
|
|
layer.cornerRadius = AppTheme.shared.buttonCornerRadius
|
|
|
|
// setup button for multiline title
|
|
titleLabel?.font = .preferredFont(forTextStyle: .body)
|
|
titleLabel?.lineBreakMode = .byWordWrapping
|
|
titleLabel?.textAlignment = .center
|
|
titleLabel?.numberOfLines = 0
|
|
titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
setTitle(titleLabel?.text?.uppercased(), for: .normal)
|
|
}
|
|
}
|
|
|
|
extension EQNRoundedButton {
|
|
class func make(
|
|
title: String = "",
|
|
target: Any,
|
|
action: Selector
|
|
) -> EQNRoundedButton {
|
|
let button = EQNRoundedButton(type: .custom)
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
button.addTarget(target, action: action, for: .touchUpInside)
|
|
button.setTitle(title.uppercased(), for: .normal)
|
|
button.setTitleColor(AppTheme.Colors.darkGray, for: .normal)
|
|
button.backgroundColor = UIColor.white.withAlphaComponent(0.5)
|
|
return button
|
|
}
|
|
}
|