Saran
Saran

Reputation: 963

iOS: Append FontAwesome icon with title in UIBarButtonItem and Button

I want a UIBarButton Item like this in my NavigationBar and

enter image description here

Normal UIButton like this

enter image description here

I already tried using these links Question 1 Question 2 Question 3 but didn't got the output.

My need is to append that FontAwesome icon with the button title text.

Can anyone help in this?

My code is:

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;

Upvotes: 4

Views: 2635

Answers (3)

user6838301
user6838301

Reputation:

Sorry for late answer but this might help someone in future,Note:Facebook denotes my UIButton

FAKFontAwesome *Gplus = [FAKFontAwesome googleIconWithSize:20];
    [Gplus addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
    NSMutableAttributedString *twitterMass = [[Gplus attributedString] mutableCopy];
    [twitterMass appendAttributedString:[[NSAttributedString alloc] initWithString:@" Login With Google" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]];
    [Facebook setAttributedTitle:twitterMass forState:UIControlStateNormal];

Upvotes: -1

In swift 2.0:

    btnAddGroup.titleLabel?.font = UIFont(name:"FontAwesome",size: 50)
    btnAddGroup.setTitle(String.fontAwesomeIconWithCode("fa-plane"), forState: .Normal)

Upvotes: 1

Meet Doshi
Meet Doshi

Reputation: 4259

If code of FontAwesome Icon is "&#xf007", then set text to @"\U0000f007" into Title.

Use Following code For UIButton:-

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

Use Following code For UIBarButton:-

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

If you want to append your string with icon then use following code:-

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];

Hope, this is what you're looking for. Any concern get back to me. :)

Upvotes: 4

Related Questions