gpt4 book ai didi

android - iOS Swift 像 Android 一样将 SVG 图像添加到按钮

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:11 25 4
gpt4 key购买 nike

在 Android 中,我们可以将 SVG 导入为 Vector XML,将其用作 Drawable,更改 SVG 图标的颜色并添加到按钮

void setSvgIcnForBtnFnc(Button setBtnVar, int setSvgVar, int setClrVar, String PosVar)
{
Drawable DevDmjVar = getDrawable(setSvgVar);
DevDmjVar.setBounds(0,0,Dpx24,Dpx24);
DevDmjVar.setColorFilter(new PorterDuffColorFilter(setClrVar, PorterDuff.Mode.SRC_IN));
switch (PosVar)
{
case "Tit" : setBtnVar.setCompoundDrawables(null, DevDmjVar, null, null); break;
case "Rit" : setBtnVar.setCompoundDrawables(null, null, DevDmjVar, null); break;
case "Pit" : setBtnVar.setCompoundDrawables(null, null, null, DevDmjVar); break;
default: setBtnVar.setCompoundDrawables(DevDmjVar, null, null, null); break;
}
}

我如何在 swift 中为 iPhone 执行此操作?

setBtnVar.setImage(<image: UIImage?>, forState: <UIControlState>)

最佳答案

UPD:另见 UseYourLoaf blog post

刚在 Erica Sadun blog post 上发现你在 iOS 11 could use Vector Assets 上。

“向量 Assets ”是什么意思:

If you click that box, the vector data will be shipped with your application. Which, on the one hand, makes your application a little bit larger, because the vector data takes up some space. But on the other hand, will give you the opportunity to scale these images, which might be useful in a number of different situations. So, one is, if you know that this particular image is going to be used at multiple sizes. But that might be less obvious. So, one case is a symbolic glyph that should resize with dynamic type. Since we're thinking about dynamic type, you should also be thinking about having glyphs that are appearing next to type resize appropriately. Another case that's really not obvious, is tab bar images. ... there's a really great accessibility feature that we strongly recommend supporting, that allows for user that have turned their dynamic type size up. ... So, we really recommend doing that to increase the usability of your app across all users

使用方法:

  1. 将您的 SVG 文件转换为 PDF,例如在 ZamZar.com
  2. 将您的 pdf 添加到 Assets.xcassets
    • 为导入的 pdf 单击“保留矢量数据”。
  3. 在您的 UIViewController 中创建 UIImageView 并分配 pdf 文件,例如 UIImage

Mac App Store 中提供的 Asset Catalog Creator 将通过简单的拖放操作完成第 1 步和第 2 步。


iOS < 11

没有使用 SVG 图像的 native 方法。

看看Macaw

通过Cocoapod导入框架

pod "Macaw", "0.8.2"

检查他们的 example project :这就是您呈现 tiger.svg 的方式(位于项目目录中,而不是在 Assets.xcassets 文件中)

import UIKit
import Macaw

class SVGExampleView: MacawView {

required init?(coder aDecoder: NSCoder) {
super.init(node: SVGParser.parse(path: "tiger"), coder: aDecoder)
}

}

当然还有一些其他的第三方库:

关于android - iOS Swift 像 Android 一样将 SVG 图像添加到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45694538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com