iOS - UIBarButtonItem 标识符 - 创建“设置”齿轮按钮的选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9755154/
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
iOS - UIBarButtonItem Identifier - option to create "settings" cogwheel button
提问by user1046037
I want to create a UIBarButtonItem to represent the app's settings (cogwheel). Presently I can only find an option to create UIBarButtonItem (Interface Builder > Attributes Inspector > identifier) such as "Add" (+), "Edit", "Done", "Cancel" etc
我想创建一个 UIBarButtonItem 来表示应用程序的设置(齿轮)。目前我只能找到一个选项来创建 UIBarButtonItem(界面生成器 > 属性检查器 > 标识符),例如“添加”(+)、“编辑”、“完成”、“取消”等
I can't find an option to create a settings (cogwheel) icon. Is there a way to do this in interface builder or through code?
我找不到创建设置(齿轮)图标的选项。有没有办法在界面构建器中或通过代码做到这一点?
Or do I have to create an image and then the image ?
还是我必须先创建一个图像,然后再创建图像?
回答by CodaFi
Unicode has several notable examples you can simply copy and paste into a string declaration in Xcode, or use the standard Unicode String Escape (\uxxxx) and iOS is actually quite fluent when it comes to Unicode (I know some of the char's are fairly ugly, but that's Unicode for ya'):
Unicode 有几个值得注意的示例,您可以简单地将其复制并粘贴到 Xcode 中的字符串声明中,或者使用标准的 Unicode String Escape (\uxxxx) 并且 iOS 在 Unicode 方面实际上非常流畅(我知道有些字符相当丑陋) ,但这是你的 Unicode'):
Unicode Character 'GEAR WITHOUT HUB' (U+26ED): http://www.fileformat.info/info/unicode/char/26ed/index.htm
Unicode 字符“GEAR WITHOUT HUB”(U+26ED):http: //www.fileformat.info/info/unicode/char/26ed/index.htm
Unicode Character 'GEAR' (U+2699): http://www.fileformat.info/info/unicode/char/2699/index.htm
Unicode 字符“齿轮”(U+2699):http: //www.fileformat.info/info/unicode/char/2699/index.htm
Or prepare an image and set the UIBarButtonItem's customView property accordingly.
或者准备一张图片并相应地设置 UIBarButtonItem 的 customView 属性。
回答by GxocT
Composing CodaFiand user1046037answers:
编写CodaFi和user1046037答案:
Creating UIBarButtonItem with unicode character as a title.
使用 unicode 字符作为标题创建 UIBarButtonItem。
You have to initialize UIBarButtonItem
with title (initWithTitle:
) not system item (initWithBarButtonSystemItem:
).
您必须UIBarButtonItem
使用标题 ( initWithTitle:
) 而不是系统项 ( initWithBarButtonSystemItem:
)进行初始化。
You can set custom title with string (such as unicode character).
您可以使用字符串(例如 unicode 字符)设置自定义标题。
You can resize title.
您可以调整标题大小。
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"\u2699" style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];
UIFont *customFont = [UIFont fontWithName:@"Helvetica" size:24.0];
NSDictionary *fontDictionary = @{NSFontAttributeName : customFont};
[settingsButton setTitleTextAttributes:fontDictionary forState:UIControlStateNormal];
回答by Murray Sagal
This works for me in Swift 3 and iOS 10...
这在 Swift 3 和 iOS 10 中对我有用......
let settingsButton = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(settings))
let font = UIFont.systemFont(ofSize: 28) // adjust the size as required
let attributes = [NSFontAttributeName : font]
settingsButton.setTitleTextAttributes(attributes, for: .normal)
See @hazernut's comment in the accepted answer. Without adding \u{0000FE0E}
to the string it shows up as an emoji and is immune to any appearance settings. Adding that string fixes that.
在接受的答案中查看@hazernut 的评论。如果不添加\u{0000FE0E}
到字符串,它会显示为表情符号,并且不受任何外观设置的影响。添加该字符串修补程序。
回答by Ε Г И ? И О
To get the cogwheel icon using Swift, do the following in viewDidLoad()
, assuming you have wired up your button from your view into your controller. (Ex: @IBOutlet var settingsButton: UIBarButtonItem!
)
要使用 Swift 获取齿轮图标,请在 中执行以下操作viewDidLoad()
,假设您已将按钮从视图连接到控制器。(例如:@IBOutlet var settingsButton: UIBarButtonItem!
)
self.settingsButton.title = NSString(string: "\u{2699}") as String
if let font = UIFont(name: "Helvetica", size: 18.0) {
self.settingsButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}