ios 如何使用Swift在ios中设置按钮上的图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36301791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the icon on the button in ios using Swift?
提问by Lovish Dogra
I want to use the icon on the button in IOS using Swift, just like the icons on the buttons in the Android. Is there any way without setting the image or without fontAwesome icon to set icon on the button?
我想使用Swift在IOS中使用按钮上的图标,就像Android中按钮上的图标一样。有没有办法不用设置图像或不用 fontAwesome 图标来设置按钮上的图标?
回答by ronatory
Via code in Swift 3 and Swift 4:
通过 Swift 3 和 Swift 4 中的代码:
yourButton.setImage(UIImage(named: "imgName"), for: .normal)
or for the background image:
或对于背景图像:
yourButton.setBackgroundImage(UIImage(named: "imgName"), for: .normal)
Via storyboard:
通过故事板:
You can also set the icon for a button via the storyboard, setting the image
in the attributes inspector
of a button element. If you want to have a title over your image set your image in background
of the attribute inspector
您还可以设置图标,通过故事板的按钮,设置image
在attributes inspector
一个按钮元素。如果您想在图像上设置标题,请background
在属性检查器中设置图像
Question from the comments
来自评论的问题
how do you put the image alongside the "button" text?
你如何将图像放在“按钮”文本旁边?
You can do that via code for example by setting the imageEdgeInsets
of the button. The example below places the image to the right from the text
您可以通过代码来实现,例如通过设置imageEdgeInsets
按钮的 。下面的示例将图像放置在文本的右侧
yourButton.setImage(UIImage(named: “imgNameWhichYouWantToSetAlongsideTheButton"), for: .normal)
yourButton.imageEdgeInsets = UIEdgeInsets(top: 3, left: 0, bottom: 0, right: -8)
回答by Nazmul Hasan
let icon = UIImage(named: "bmiCalculator")!
button.setImage(icon, for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
UIEdgeInsets
UIEdgeInsets
The inset distances for views. Edge inset values are applied to a rectangle to shrink or expand the area represented by that rectangle. Typically, edge insets are used during view layout to modify the view's frame. Positive values cause the frame to be inset (or shrunk) by the specified amount. Negative values cause the frame to be outset (or expanded) by the specified amount.
视图的插入距离。边缘插入值应用于矩形以缩小或扩展由该矩形表示的区域。通常,在视图布局期间使用边缘插入来修改视图的框架。正值会导致帧插入(或收缩)指定的量。负值会导致框架开始(或扩展)指定的数量。
回答by ios
Use this code to set image (icon) to your button .
使用此代码将图像(图标)设置为您的按钮。
myButton.setImage(UIImage(named: "nameOfImage.png"), forState: UIControlState.Normal)
回答by derdida
For FontAwesome you could use a plugin on github.
对于 FontAwesome,您可以使用 github 上的插件。
https://github.com/thii/FontAwesome.swift
https://github.com/thii/FontAwesome.swift
Then you are able to set icons quite easy:
然后你就可以很容易地设置图标:
button.titleLabel?.font = UIFont.fontAwesomeOfSize(30)
button.setTitle(String.fontAwesomeIconWithName(.Github), forState: .Normal)
And dont need to care about image resolution for different devices (x1, x2, x3) because fonts are vectors.
并且不需要关心不同设备(x1、x2、x3)的图像分辨率,因为字体是矢量。
回答by Micah Montoya
If you don't want to use a 3rd party plugin you can just do
如果您不想使用第 3 方插件,您可以这样做
if let font = UIFont(name: "FontAwesomeFontName", size: 18) {
button.titleLabel?.font = font
button.setTitle("\u{f053}", for: .normal)
}
And use the fontawesome cheatsheetfor reference for the specific icon you are wanting.
并使用fontawesome 备忘单作为您想要的特定图标的参考。
回答by Marco Weber
The following solution is for centered button, because the icon will always be displayed right next to the title-label.
以下解决方案针对居中按钮,因为图标将始终显示在标题标签旁边。
I build an easy extension for it (I used the IonIcons-Font, but obviously you can use any kind of font e.g. Font-Awesome):
我为它构建了一个简单的扩展(我使用了IonIcons-Font,但显然你可以使用任何类型的字体,例如Font-Awesome):
extension UIButton {
func addIcon(icon: String, font:String = IonIcons.fontName.rawValue, fontSize: CGFloat = 16, text: String, color:UIColor) {
let iconWithPlaceholder = icon + " "
let iconRange = (iconWithPlaceholder as NSString).range(of: icon)
let attributedString = NSMutableAttributedString(string: iconWithPlaceholder + text)
attributedString.addAttributes([NSAttributedString.Key.font: UIFont(name: IonIcons.fontName.rawValue, size: fontSize) as Any], range: iconRange)
self.setAttributedTitle(attributedString, for: .normal)
self.tintColor = color
}
}
So you can easily call it from your whole project like this:
因此,您可以像这样轻松地从整个项目中调用它:
self.myButton.addIcon(icon: IonIcons.navigateOutline.rawValue, text: "Start", color: .white)
回答by Sanjay Mishra
In Swift-5...
在 Swift-5 中...
btnNavBar.titleLabel?.font = UIFont(name: "Font Awesome 5 Free", size: 20)
btnNavBar.setTitle("\u{f007}", for: .normal)