【问题标题】:Resizing UImage when using SF Symbols - UIImage(systemName:)使用 SF 符号时调整 UImage 的大小 - UIImage(systemName:)
【发布时间】:2020-07-09 15:37:19
【问题描述】:

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

UIImage(systemName: "location.fill")

这可能看起来微不足道,但我如何调整它的大小以使其更大?谢谢。

【问题讨论】:

    标签: swift uiimage uikit image-resizing sf-symbols


    【解决方案1】:

    您可以实例化UIImage.SymbolConfiguration。例如,您可以指定磅值:

    let configuration = UIImage.SymbolConfiguration(pointSize: 24)
    let image = UIImage(systemName: "location.fill", withConfiguration: configuration)
    

    UIFont.TextStyle:

    let configuration = UIImage.SymbolConfiguration(textStyle: .title1)
    let image = UIImage(systemName: "location.fill", withConfiguration: configuration)
    

    使用后一种方法,它将遵循设备上的可访问性文本大小设置。

    【讨论】:

      【解决方案2】:

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

      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")
                  }
            }
        

      【讨论】:

      • 我的图像需要像按钮一样工作,Image 类可以工作吗?
      • 不幸的是,我正在使用使用 UIView 的视图控制器。这需要使用我不能使用的普通视图。有什么想法吗?
      • 用两种不同的方法更新了答案。
      • 这段代码大部分与问题无关
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2019-01-20
      • 2022-12-14
      • 2012-02-04
      • 1970-01-01
      相关资源
      最近更新 更多