|
// |
|
// RoundButton.swift |
|
// |
|
// Created by Bruno Scheltzke on 04/06/20. |
|
// Copyright © 2020 Bruno Scheltzke All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
@IBDesignable |
|
class RoundButton: UIButton { |
|
|
|
override var bounds: CGRect { |
|
didSet { |
|
setup() |
|
} |
|
} |
|
|
|
@IBInspectable var cornerRadius: CGFloat { |
|
set { |
|
layer.cornerRadius = newValue |
|
clipsToBounds = newValue > 0 |
|
} |
|
get { |
|
return layer.cornerRadius |
|
} |
|
} |
|
|
|
override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
setup() |
|
} |
|
|
|
required init?(coder: NSCoder) { |
|
super.init(coder: coder) |
|
setup() |
|
} |
|
|
|
func setup() { |
|
cornerRadius = frame.height/2.2 |
|
tintColor = .white |
|
backgroundColor = .systemBlue |
|
titleLabel?.font = .systemFont(ofSize: 19) |
|
layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor |
|
layer.shadowOffset = CGSize(width: 0.0, height: 2.0) |
|
layer.shadowOpacity = 1.0 |
|
layer.shadowRadius = 0.0 |
|
layer.masksToBounds = false |
|
} |
|
|
|
override var intrinsicContentSize: CGSize { |
|
let height: CGFloat = 50 |
|
var width: CGFloat = 100 |
|
if let font = self.titleLabel?.font, |
|
let calculatedWidth = titleLabel?.text?.width(withConstrainedHeight: height, font: font), |
|
calculatedWidth != 0 { |
|
width = calculatedWidth + 120 |
|
} |
|
return CGSize(width: width, height: height) |
|
} |
|
|
|
} |
|
|
|
@IBDesignable |
|
class PrimaryRoundButton: RoundButton { |
|
|
|
override func setup() { |
|
super.setup() |
|
backgroundColor = .orange |
|
} |
|
|
|
} |
|
|
|
extension String { |
|
|
|
func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat { |
|
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height) |
|
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) |
|
|
|
return ceil(boundingBox.width) |
|
} |
|
|
|
} |