iOS:在 UIBarButtonItem 和 Button 中附加带有标题的 FontAwesome 图标

标签 ios objective-c uibutton uibarbuttonitem font-awesome

我想要在我的 NavigationBar 中有一个像这样的 UIBarButton Item

enter image description here

像这样的普通 UIButton

enter image description here

我已经尝试过使用这些链接 Question 1 Question 2 Question 3但没有得到输出。

我需要在 FontAwesome 图标上附加按钮标题文本。

有人可以帮忙吗?

我的代码是:

NSString *icon = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-folder-open"];

NSString *locationString = [NSString stringWithFormat:@"%@ %@", icon, @"Change"];

NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:locationString];

[astring addAttribute:NSFontAttributeName
                value:[UIFont iconicFontOfSize:20]
                range:NSMakeRange(0,1)]; // The first character

changeFolderButton.titleLabel.attributedText = astring;

最佳答案

如果代码为FontAwesome图标是"&#xf007" , 然后将文本设置为 @"\U0000f007"进入标题。

UIButton 使用以下代码:-

myBtn.titleLabel.font =[UIFont fontWithName:@"FontAwesome" size:20.0];
[myBtn setTitle:@"\U0000f007" forState:UIControlStateNormal]];

UIBarButton 使用以下代码:-

[self.barButton setTitleTextAttributes:@{
              NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
              NSForegroundColorAttributeName: self.view.tintColor
                                     } forState:UIControlStateNormal]; 
[self.barButton setTitle:@"\U0000f007"]];

如果你想用图标附加你的字符串,那么使用下面的代码:-

NSString * myString = @"\U0000f007 Change";
NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:myString];
[astring addAttribute:NSFontAttributeName
                value:[UIFont fontWithName:@"FontAwesome" size:22.0]
                range:NSMakeRange(0,1)]; //If Icon is on starting position
[myBtn setAttributedTitle:astring forState:UIControlStateNormal];

希望,这就是您要找的。有任何疑虑,请回复我。 :)

关于iOS:在 UIBarButtonItem 和 Button 中附加带有标题的 FontAwesome 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468888/

相关文章:

ios - 如何制作像gomoji应用程序一样的2D模型(可移动)?

objective-c - 强制 NStextField 只接受十六进制值

ios - 如何插入新字体并强制更改 UIButton?

iphone - 使用 Core Data 和 NSManagedObject 保存列表

objective-c - 带有 UILabel subview 缓存问题的 UITableViewCell

iphone - 点击屏幕锁定按钮时如何完全退出我的应用程序?

iphone - 多个 View Controller 中的 iAd 示例

iphone - UITableView cellForRowAtIndexPath 导致滚动缓慢

ios - 在 iOS 中将文本更改为 uibutton

ios - 当包含在 UIBarButtonItem 中时增加 UIButton 的固有大小