我想要在我的 NavigationBar 中有一个像这样的 UIBarButton Item
像这样的普通 UIButton
我已经尝试过使用这些链接 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
图标是""
, 然后将文本设置为 @"\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/