在UIBarButtonItem上的ios-fontawesome
我正在尝试使用iOS FontAwesome在条形button上设置图标,如下所示:
[self.barButton setTitle:[NSString fontAwesomeIconStringForEnum:FACamera]];
也与此:
[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
无论我使用什么标识符,结果都是一样的:
什么可能是错的?
如果有人想知道:
[self.barButton setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0], NSForegroundColorAttributeName: self.view.tintColor } forState:UIControlStateNormal]; [self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
在Swift中:
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
以防万一你想使用更简单的解决scheme,而不使用属性文本,看看我的图书馆。 cocoa豆荚也被支持。 字体真棒Swift
使用FontAwesomeKit 2.2 API,你可以这样做:
[self.barButton setTitleTextAttributes:@{ NSFontAttributeName: [FAKFontAwesome iconFontWithSize:20] } forState:UIControlStateNormal]; [self.barButton setTitle:[FAKFontAwesome cameraIconWithSize:20].attributedString.string];
或者像FontAwesomeKit文档中build议的那样转换成UIImage
:
FAKFontAwesome *cameraIcon = [FAKFontAwesome cameraIconWithSize:20]; UIImage *cameraImage = [cogIcon imageWithSize:CGSizeMake(20, 20)]; [self.barButton setImage:camera];