2021-02-01 11:06:29 +01:00
|
|
|
//
|
|
|
|
// UIButton.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/1.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension UIButton {
|
|
|
|
func setInsets(
|
|
|
|
forContentPadding contentPadding: UIEdgeInsets,
|
|
|
|
imageTitlePadding: CGFloat
|
|
|
|
) {
|
|
|
|
switch UIApplication.shared.userInterfaceLayoutDirection {
|
|
|
|
case .rightToLeft:
|
|
|
|
self.contentEdgeInsets = UIEdgeInsets(
|
|
|
|
top: contentPadding.top,
|
|
|
|
left: contentPadding.left + imageTitlePadding,
|
|
|
|
bottom: contentPadding.bottom,
|
|
|
|
right: contentPadding.right
|
|
|
|
)
|
|
|
|
self.titleEdgeInsets = UIEdgeInsets(
|
|
|
|
top: 0,
|
|
|
|
left: -imageTitlePadding,
|
|
|
|
bottom: 0,
|
|
|
|
right: imageTitlePadding
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
self.contentEdgeInsets = UIEdgeInsets(
|
|
|
|
top: contentPadding.top,
|
|
|
|
left: contentPadding.left,
|
|
|
|
bottom: contentPadding.bottom,
|
|
|
|
right: contentPadding.right + imageTitlePadding
|
|
|
|
)
|
|
|
|
self.titleEdgeInsets = UIEdgeInsets(
|
|
|
|
top: 0,
|
|
|
|
left: imageTitlePadding,
|
|
|
|
bottom: 0,
|
|
|
|
right: -imageTitlePadding
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
extension UIButton {
|
|
|
|
// https://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted
|
|
|
|
private func image(withColor color: UIColor) -> UIImage? {
|
|
|
|
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
|
|
|
|
UIGraphicsBeginImageContext(rect.size)
|
|
|
|
let context = UIGraphicsGetCurrentContext()
|
|
|
|
|
|
|
|
context?.setFillColor(color.cgColor)
|
|
|
|
context?.fill(rect)
|
|
|
|
|
|
|
|
let image = UIGraphicsGetImageFromCurrentImageContext()
|
|
|
|
UIGraphicsEndImageContext()
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBackgroundColor(_ color: UIColor, for state: UIControl.State) {
|
|
|
|
self.setBackgroundImage(image(withColor: color), for: state)
|
|
|
|
}
|
|
|
|
}
|