我有以下预打包的系统SF映像:
UIImage(systemName: "location.fill")
这看起来微不足道,但我如何调整它的大小,使它更大?谢谢。
gstyhher1#
可以示例化UIImage.SymbolConfiguration。例如,可以指定点大小:
UIImage.SymbolConfiguration
let configuration = UIImage.SymbolConfiguration(pointSize: 24) let image = UIImage(systemName: "location.fill", withConfiguration: configuration)
或UIFont.TextStyle:
UIFont.TextStyle
let configuration = UIImage.SymbolConfiguration(textStyle: .title1) let image = UIImage(systemName: "location.fill", withConfiguration: configuration)
使用后一种方法,它将荣誉设备上的辅助功能文本大小设置。
s71maibg2#
下面的代码提供了两个方法:
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") } }
5lhxktic3#
Rob的回答很棒,你可以和督察谈谈
3条答案
按热度按时间gstyhher1#
可以示例化
UIImage.SymbolConfiguration
。例如,可以指定点大小:或
UIFont.TextStyle
:使用后一种方法,它将荣誉设备上的辅助功能文本大小设置。
s71maibg2#
下面的代码提供了两个方法:
1.一种SwiftUI方式。按钮(以及示例中的一些关联文本)缩放到提供的框架。
5lhxktic3#
Rob的回答很棒,你可以和督察谈谈