macos 右键菜单自定义
If you’re a genius like me, you need many days of scouring the web and weeping in the night to learn new things like… Swift and SwiftUI.
如果您是像我这样的天才,则需要花费很多时间在网上搜寻和夜间哭泣,才能学习诸如Swift和SwiftUI之类的新事物。
In this little tutorial, we’ll learn to:
在这个小教程中,我们将学习:
Create a nice little “image button” that calls a function you’ve passed to it. 创建一个漂亮的小“图像按钮”,以调用传递给它的函数。 And shows the image in 32 x 32 no matter what the size of the image was. 并以32 x 32的分辨率显示图像,无论图像大小如何。 And is dynamically colored with your own custom color. 并使用您自己的自定义颜色动态着色。The end result is shown below. You click the button, and whatever function you passed to the view gets called. The blue you see is a custom blue.
最终结果如下所示。 您单击按钮,传递给视图的任何函数都会被调用。 您看到的蓝色是自定义蓝色。
Here’s how you would use the code:
这是使用代码的方式:
VImgButton(callback: { print ("hello") } , imageName: “alarm”)
VImgButton(callback: { print ("hello") } , imageName: “alarm”)
Let’s get to the code, shall we? Let’s first create an Image Button.
让我们看一下代码,好吗? 首先创建一个图像按钮。
struct VImgButton: View {// callback is the function you want to be called back when// the button is pressedvar callback: () -> Void// An image from the system to usevar imageName: String = "folder"var body: some View {Button(action: {self.callback()}) { VStdImg(name: imageName) // a nicely formatted image }.buttonStyle(BorderlessButtonStyle()) //don't need no stinkin' borders to this button }}What the hell is VStdImg? Well, it's a short view that creates a standardized image that's of size 32 x 32no matter what the size of the image is. This is done by wrapping the image in a VStack of size 32 x 32, and telling the image to resize and "fill" that space.
VStdImg到底是VStdImg ? 好吧,这是一个简短的视图,可创建大小为32 x 32的标准化图像,而不管图像的大小如何。 这是通过将图像包装在大小为32 x 32的VStack中并告诉图像调整大小并“填充”该空间来完成的。
func VStdImg(name: String, color: Color = .AppBlue) -> some View {return VStack { Image(name) .resizable() .renderingMode(.template) // this one helps resize the image .foregroundColor(color) .aspectRatio(1, contentMode: .fit) // keep the aspect ratio }.frame(width: 32, height: 32) // the image will fit this size}So, at this point, you have an VImgButton View ready for use. How do you use it?
因此,此时,您已经可以使用VImgButton视图了。 你如何使用它?
Well, simple, use it wherever, like this:
好吧,很简单,可以在任何地方使用它,就像这样:
VImgButton(callback: { print("oh yeah!") }, imageName: "alarm")And that’s it! You got yourself a pretty button. Wait, what about that custom color I talked about?
就是这样! 你有一个漂亮的按钮。 等等,我说的那种自定义颜色呢?
Look at the signature of VStdImg - I have a default set to AppBlue - there's no system color called AppBlue, it's mine! Mine! Mine!
查看VStdImg的签名-我将默认设置设置为AppBlue没有名为AppBlue的系统颜色,这是我的! 矿! 矿!
Here’s how you get yourself a custom color.
这是您如何获得自定义颜色的方法。
Create a new Image set in your Xcode Assets.xcassets- give it whatever name you want (like AppBlue)
在您的Xcode Assets.xcassets创建一个新的图像集-为其提供任何您想要的名称(例如AppBlue )
Next, create an extension for Color, like this: 接下来,为Color创建一个扩展名,如下所示: extension Color {static var AppOrange = Color("AppOrange")static var AppBlue = Color("AppBlue")}And use it like this:
并像这样使用它:
.foregroundColor(.AppBlue)That’s it! Hope you found that useful.
而已! 希望您觉得有用。
Originally published at https://www.jaypenner.com on May 11, 2020.
最初于 2020年5月11日 发布在 https://www.jaypenner.com 上。
翻译自: https://medium.com/macoclock/swiftui-macos-create-image-button-with-custom-color-e4f3dbadf67e
macos 右键菜单自定义