简体   繁体   English

如何在通用图标 class 中一起管理自定义导入图标和 sf 符号的大小调整?

[英]How to manage the resizing of a custom imported icon and sf symbol together in a common icon class?

I am creating a library icon class which receives an icon name, a flag 'isSF' (this flag will say whether the icon is SF Symbol or not) and the size of the icon.我正在创建一个库图标 class ,它接收图标名称、标志“isSF”(此标志将说明图标是否为 SF 符号)和图标的大小。 I need to resize both the icons as per the size value received.我需要根据收到的大小值调整两个图标的大小。 The issue I am facing here is, for SF Symbols, we can resize it using the.font(.system(size: value)) modifier but how to resize the image (custom icon) similarly.我在这里面临的问题是,对于 SF Symbols,我们可以使用 .font(.system(size: value)) 修饰符调整它的大小,但如何调整图像(自定义图标)的大小。

Kindly help me.请帮助我。 Thanks in advance提前致谢

struct XIcon: View {
    var iconName: String
    var isSF: Bool? = true
    var size: CGFloat
    var body: some View {
        if isSF! {
            return AnyView( Image(systemName: "\(iconName)")
                .font(.system(size: size)))
        } else {
            return AnyView( Image(uiImage: UIImage(named: "\(iconName)")!).resizable())
        }
    }
}

Again, i know when we specify size, the vector alignment is carried out but for frame() it is not the case.同样,我知道当我们指定大小时,执行向量 alignment 但对于 frame() 情况并非如此。 For image scaling, scaleEffect() is there but i don't know how to scale the image equivalent to the size specified for SF Symbol对于图像缩放,有 scaleEffect() 但我不知道如何缩放图像,使其与为 SF Symbol 指定的大小相等

Here is possible solution.这是可能的解决方案。 Tested with Xcode 11.4 / iOS 13.4使用 Xcode 11.4 / iOS 13.4 测试

演示

Note: Borders are for better size visibility注意:边框是为了更好地显示尺寸

struct XIcon: View {
    var iconName: String
    var isSF: Bool? = true
    var size: CGFloat

    var body: some View {
        var image: Image
        if isSF! {
            image = Image(systemName: "\(iconName)")
        } else {
            image = Image(uiImage: UIImage(named: "\(iconName)")!)
        }
        return image.resizable()
            .frame(width: size, height: size).aspectRatio(contentMode: .fill)
    }
}

struct TestXIcon: View {
    var body: some View {
        VStack {
            XIcon(iconName: "tv", size: 100)
                .border(Color.red)
            XIcon(iconName: "icon", isSF: false, size: 100)
                .border(Color.red)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM