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

标签 android ios swift image svg

在 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/

相关文章:

ios - 两行首字母非常大的 UILabel 或 UITextView

java - 对话框未从 EditText 框中提取信息

android - 单击按钮时展开可扩展 ListView

java - 当另一个声音播放时停止播放上一个声音

ios - 如何使用 iCloud 同步 iOS 和 OS X 应用程序?

ios - 无法实例化名为 IBNSLayoutConstraint 的类

ios - 如何在 Swift SpriteKit 中访问另一个 SKScene 的变量

android - 8.9 英寸平板电脑上没有静态尺寸且没有低分辨率图像的 LinearLayout

ios - Swift 屏幕截图观察者执行每个 session 查看 viewController 的次数(不是应该的一次)

ios - MapBox 路线进度 RouteStepProgress 变量 moSTLy 返回 nil