UIKit开发中如何同时修改SF Symbol的尺寸与颜色?
你只需要在配置完SF Symbol的尺寸、权重、缩放参数后,额外指定着色配置即可,两类配置互相独立不会互相覆盖,有两种常用实现方案:
方案1:直接给NSImage设置tint
// 先配置SF Symbol的尺寸、权重、缩放参数 let plusSymbol = NSImage(named: "plus.circle.filled")? .withSymbolConfiguration(.init(pointSize: 15, weight: .bold, scale: .large)) // 单独配置tint颜色,和上面的Symbol配置不冲突 plusSymbol?.tintColor = .systemBlue // 替换为你需要的自定义颜色 self.Button = makeToolBarButton( buttonTitle: "plus", buttonFont: NSFont.systemFont(ofSize: buttonSize), image: plusSymbol, { button in button.params["actionType"] = ButtonType.image } )
方案2:给按钮层级设置tint(支持动态修改)
如果后续需要动态切换按钮的着色状态,建议直接给NSButton本身设置contentTintColor,不需要重新生成图片:
let plusSymbol = NSImage(named: "plus.circle.filled")? .withSymbolConfiguration(.init(pointSize: 15, weight: .bold, scale: .large)) self.Button = makeToolBarButton( buttonTitle: "plus", buttonFont: NSFont.systemFont(ofSize: buttonSize), image: plusSymbol, { button in button.params["actionType"] = ButtonType.image // 按钮层级的tint配置优先级更高,可随时修改 button.contentTintColor = .systemRed } )
两种方案均兼容 macOS 10.14 及以上系统版本,你可以根据自己的使用场景选择。
内容的提问来源于stack exchange,提问作者sobrevillaj
相关产品推荐
相关产品推荐

