使用SF符号时调整UImage的大小-UIImage(systemName:)

问题描述 投票:-1回答:1

我具有以下预打包的系统SF映像:

UIImage(systemName: "location.fill")

看似微不足道,但如何调整大小使其更大?谢谢。

swift uiimage uikit image-resizing sf-symbols
1个回答
0
投票

下面的代码提供了两种方法:

  1. UIKit建议的方式(请参见developer.apple.com/documentation)。在这里,您将SF Symbol图像视为一种字体,并为其应用磅值。

  2. 一种SwiftUI方式。按钮(以及示例中的一些关联文本)缩放到所提供的框架。

        import UIKit
        import SwiftUI
    
        class SwiftUIButton : ObservableObject {
    
              @Published var state : Bool = true { didSet { buttonDidChange(state) } }
    
              private lazy var viewController = UIHostingController(rootView: SwiftCustomButton(delegate: self))
              var view : UIView { guard let view = viewController.view else {
                    preconditionFailure("*Fatal Error* Could not create SwiftUI button") }
                    return view
              }
    
              typealias typeForAction = () -> Void
              var action : typeForAction?
    
              func buttonDidChange(_ state : Bool) {
                    guard let action = action else {
                          return
                    }
                    action()
              }
        }
    
        struct SwiftCustomButton : View {
    
              @ObservedObject var delegate : SwiftUIButton
    
              var body : some View {
                    VStack() {
                          Button(action: {
                                self.delegate.state = !self.delegate.state
                          }) { Image(systemName: "gear")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                          }
    
                          Text(delegate.state ? "Hello" : "Goodbye")
                    }
              }
        }
    
    
        class ViewController: UIViewController {
    
              override func viewDidLoad() {
                    super.viewDidLoad()
    
                    let button = SwiftUIButton()
                    button.action = { print("In the ViewController the button has changed to: \(button.state)")}
                    button.view.frame = CGRect(x: 200, y: 200, width: 80, height: 80)
                    view.addSubview(button.view)
    
                    let image = UIImage(systemName: "gear")
                    let symbolConfiguration = UIImage.SymbolConfiguration(pointSize: 64.0)
    
                    let adjustedImage = image?.applyingSymbolConfiguration(symbolConfiguration)
    
                    let uButton = UIButton(type: .custom)
                    uButton.setImage(adjustedImage , for: .normal)
                    uButton.frame = CGRect(x: 20, y: 200, width: 80, height: 80)
                    uButton.target(forAction: #selector(self.buttonAction), withSender: uButton)
                    view.addSubview(uButton)
    
                    self.view = view
              }
    
              @objc func buttonAction() {
                    print("UIButton pressed")
              }
        }
    
© www.soinside.com 2019 - 2024. All rights reserved.