4

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;
Community
  • 1
  • 1
Saran
  • 953
  • 2
  • 10
  • 15

3 Answers3

4

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. :)

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
  • :: if myBtn title is some icon then where can i append that "Change" – Saran Dec 26 '15 at 06:18
  • :: I want that icon + text(change) as title for that button – Saran Dec 26 '15 at 06:19
  • i cant able to use like this " [changeFolderButton setTitle:@"\U0000f015"+@"change" forState:UIControlStateNormal]; " – Saran Dec 26 '15 at 06:21
  • If you want to set FontAwesome into Button's Title, then you have to set that button's font to @"FontAwesome". Now, You want to set some text with append that Icon, then for that, you have to use another label behind that Button. Because we already changed the font of that button. – Meet Doshi Dec 26 '15 at 06:27
  • pls have a look on this http://stackoverflow.com/questions/20894697/ios-fontawesome-in-middle-of-other-string – Saran Dec 26 '15 at 06:29
  • yess... otherwise you can use NSMutableAttributedString for this. – Meet Doshi Dec 26 '15 at 06:34
  • can u pls help me how to use that method? – Saran Dec 26 '15 at 06:36
1

In swift 2.0:

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

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