ios-fontawesome on UIBarButtonItem

2019-02-19 18:44发布

I'm trying to use iOS FontAwesome to set an icon on a bar button, like this:

[self.barButton setTitle:[NSString fontAwesomeIconStringForEnum:FACamera]];

and also with this:

[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];

No matter what identifier I use the result is the same:

enter image description here

What could be wrong?

4条回答
欢心
2楼-- · 2019-02-19 19:01

In case anyone else ever wonders:

[self.barButton setTitleTextAttributes:@{
                  NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
                  NSForegroundColorAttributeName: self.view.tintColor
                                         } forState:UIControlStateNormal]; 
[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
查看更多
Luminary・发光体
3楼-- · 2019-02-19 19:02

Just in case you want to use easier solution without using attributed texts, look at my library. Cocoa pods are also supported. Font Awesome Swift

查看更多
成全新的幸福
4楼-- · 2019-02-19 19:04

With the FontAwesomeKit 2.2 API, you can do it like this:

[self.barButton setTitleTextAttributes:@{
    NSFontAttributeName: [FAKFontAwesome iconFontWithSize:20]
} forState:UIControlStateNormal];
[self.barButton setTitle:[FAKFontAwesome cameraIconWithSize:20].attributedString.string];

Or convert to UIImage like suggested in the FontAwesomeKit documentation:

FAKFontAwesome *cameraIcon = [FAKFontAwesome cameraIconWithSize:20];
UIImage *cameraImage = [cogIcon imageWithSize:CGSizeMake(20, 20)];
[self.barButton setImage:camera];
查看更多
爷的心禁止访问
5楼-- · 2019-02-19 19:12

In 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
查看更多
登录 后发表回答