// // Copyright (c) 2023 Touch Instinct // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the Software), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // import TIUIKitCore import UIKit extension UIButton { public func configureUIButton(appearance: BaseAppearance, for state: UIControl.State = .normal) { appearance.textAttributes? .configure(button: self, with: title(for: state), for: state) if #available(iOS 15, *) { var config = configuration ?? .plain() configureInsets(in: &config, contentInsets: appearance.contentLayout.contentInsets.replacingNan(with: .zero), titleInsets: appearance.contentLayout.titleInsets.replacingNan(with: .zero), imageInsets: appearance.contentLayout.imageInsets.replacingNan(with: .zero)) configuration = config } else { contentEdgeInsets = appearance.contentLayout.contentInsets.replacingNan(with: .zero) titleEdgeInsets = appearance.contentLayout.titleInsets.replacingNan(with: .zero) imageEdgeInsets = appearance.contentLayout.imageInsets.replacingNan(with: .zero) } super.configureUIView(appearance: appearance) } @available(iOS 15.0, *) private func configureInsets(in config: inout UIButton.Configuration, contentInsets: UIEdgeInsets, titleInsets: UIEdgeInsets, imageInsets: UIEdgeInsets) { let topContentInset = contentInsets.top + titleInsets.top let bottomContentInset = contentInsets.bottom + titleInsets.bottom let fullLeadingContentInset = contentInsets.left + titleInsets.left let fullTrailingContentInset = contentInsets.right + titleInsets.right let hasNonEmptyImage = [ config.image, currentImage ] .compactMap { $0 } .contains { !($0.size.width.isZero || $0.size.height.isZero) } if hasNonEmptyImage { let leadingContentInset: CGFloat let trailingContentInset: CGFloat let imagePadding: CGFloat switch config.imagePlacement { case .leading: leadingContentInset = contentInsets.left trailingContentInset = fullTrailingContentInset imagePadding = titleInsets.left + imageInsets.right case .trailing: leadingContentInset = fullLeadingContentInset trailingContentInset = contentInsets.right imagePadding = titleInsets.right + imageInsets.left default: leadingContentInset = fullLeadingContentInset trailingContentInset = fullTrailingContentInset imagePadding = .zero } config.contentInsets = NSDirectionalEdgeInsets(top: topContentInset, leading: leadingContentInset, bottom: bottomContentInset, trailing: trailingContentInset) config.imagePadding = imagePadding } else { config.contentInsets = NSDirectionalEdgeInsets(top: topContentInset, leading: fullLeadingContentInset, bottom: bottomContentInset, trailing: fullTrailingContentInset) } } }