我正在尝试使用iOS FontAwesome在条形按钮上设置图标,如下所示:
[self.barButton setTitle:[NSString fontAwesomeIconStringForEnum:FACamera]];
还有这个:
[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
无论我使用什么标识符,结果都是相同的:
可能出现什么问题?
答案 0 :(得分:5)
万一其他人想知道:
[self.barButton setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
NSForegroundColorAttributeName: self.view.tintColor
} forState:UIControlStateNormal];
[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
答案 1 :(得分:1)
在斯威夫特:
let filterButton = UIBarButtonItem(title: String.fontAwesomeIconWithName(FontAwesome.Filter), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("filterClicked"))
filterButton.setTitleTextAttributes(NSDictionary(dictionary: [NSFontAttributeName:UIFont.fontAwesomeOfSize(25), NSForegroundColorAttributeName : UIColor.COLOR_BLUE_DARK]), forState: UIControlState.Normal)
self.navigationItem.rightBarButtonItem = filterButton
答案 2 :(得分:1)
使用FontAwesomeKit 2.2 API,您可以这样做:
[self.barButton setTitleTextAttributes:@{
NSFontAttributeName: [FAKFontAwesome iconFontWithSize:20]
} forState:UIControlStateNormal];
[self.barButton setTitle:[FAKFontAwesome cameraIconWithSize:20].attributedString.string];
或转换为UIImage
,如FontAwesomeKit documentation中建议的那样:
FAKFontAwesome *cameraIcon = [FAKFontAwesome cameraIconWithSize:20];
UIImage *cameraImage = [cogIcon imageWithSize:CGSizeMake(20, 20)];
[self.barButton setImage:camera];
答案 3 :(得分:0)
在 Swift 4 中,这对我有用
let button1 = UIBarButtonItem(title: "\u{f05a}", style: .plain, target: self, action: #selector(showCartInfoPopUp(_:)))
button1.setTitleTextAttributes([.font : UIFont(name: "Font Awesome 5 Pro", size: 24), .foregroundColor : UIColor.white], for: .normal)
self.navigationItem.rightBarButtonItem = button1