0

I am new in iOS. And I am facing a problem to add font awesome on UINavigation Bar. I am using code for Image Like this

  UIImage *listImage2 = [UIImage imageNamed:@"Image1.png"];
    UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect listButton2Frame = listButton2.frame;
    listButton2Frame.size = listImage2.size;
    listButton2.frame = listButton2Frame;

    [listButton2 setImage:listImage2 forState:UIControlStateNormal];
    [listButton2 addTarget:self
                    action:@selector(LogoutClick:)
          forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton2 =
    [[UIBarButtonItem alloc] initWithCustomView:listButton2];


    UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"];
    UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom];

    // get the image size and apply it to the button frame
    CGRect listButton4Frame = listButton4.frame;
    listButton4Frame.size = listImage4.size;
    listButton4.frame = listButton4Frame;

    [listButton4 setImage:listImage4 forState:UIControlStateNormal];
    [listButton4 addTarget:self
                    action:@selector(ActualNotificationClick:)
          forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton4 =
    [[UIBarButtonItem alloc] initWithCustomView:listButton4];

    UIImage *listImage3 = [UIImage imageNamed:@"Image3.png"];
    UIButton *listButton3 = [UIButton buttonWithType:UIButtonTypeCustom];

    // get the image size and apply it to the button frame
    CGRect listButton3Frame = listButton3.frame;
    listButton3Frame.size = listImage3.size;
    listButton3.frame = listButton3Frame;

    [listButton3 setImage:listImage3 forState:UIControlStateNormal];
    [listButton3 addTarget:self
                    action:@selector(EmployeeClick:)
          forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton3 =
    [[UIBarButtonItem alloc] initWithCustomView:listButton3];

    self.navigationItem.rightBarButtonItems= [NSArray arrayWithObjects:jobsButton2,jobsButton4, nil];
    self.navigationItem.leftBarButtonItems=[NSArray arrayWithObjects:jobsButton3, nil];

and for font awesome I am using code like this

lab.font = [UIFont fontWithName:@"FontAwesome" size:25];
lab.textColor =  [UIColor whiteColor];
lab.text =  [NSString awesomeIcon:FaCalendarChecko]; 

I need to add the font awesome Label instead of image on UINavigation Bar. The Image I am using is look like this enter image description here

I have Add Font Awesome like this code

newjoinlbl.font = [UIFont fontWithName:@"FontAwesome" size:8];
    newjoinlbl.textColor =  [UIColor whiteColor];
    newjoinlbl.text =  [NSString awesomeIcon:FaChild];
    UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom];

    // get the image size and apply it to the button frame
    CGRect listButton2Frame = listButton2.frame;
    listButton2Frame.size = listImage2.size;
    listButton2.frame = listButton2Frame;

    [listButton2 setTitle:newjoinlbl.text forState:UIControlStateNormal];
    [listButton2 addTarget:self
                    action:@selector(LogoutClick:)
          forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton2 =
    [[UIBarButtonItem alloc] initWithCustomView:listButton2];

But It give me out put like this

enter image description here

Font awesome is work proper in view enter image description here

I just need to add font awesome label instead of Image on UINAvigation Bar. Thanks in Advance!

Muju
  • 884
  • 20
  • 54
  • you can Hide your navigationBar, and then you can create your own Navigationbar using View and Label and buttons for that view controller in which you need to do above implementation. – Wolverine Oct 25 '16 at 05:52
  • @Wolverine But that not work same as UINavigation Bar. – Muju Oct 25 '16 at 05:53
  • NavigationBar is used to Show Title, and buttons. And if you need this level of customisation i suggest to create your own view which will look like your desired output. I am not saying to remove navigation, I am saying Hide NavigatioBar and then show your own view. – Wolverine Oct 25 '16 at 05:56
  • http://stackoverflow.com/questions/10569878/how-to-integrate-and-use-font-awesome-with-objective-c-or-swift-in-an-xcode-proj – Krutarth Patel Oct 25 '16 at 06:01
  • http://fontawesome.io/3.2.1/cheatsheet/ – Krutarth Patel Oct 25 '16 at 06:02
  • do you want that label with custom font as screen title ? – Ketan Parmar Oct 25 '16 at 06:03
  • @KrutarthPatel I have already add font awesome in my app. Just need to add it to UINavigation bar instead of image. – Muju Oct 25 '16 at 06:04
  • So, you want to add a title to the UINavigationBar instead of these images right? – KrishnaCA Oct 25 '16 at 06:04
  • @Lion Just need to add UILabel hear [UIImage imageNamed:@"Image2.png"]; see in the code. – Muju Oct 25 '16 at 06:05
  • @KrishnaChaitanyaAmjuri I need to add Button and on Button Label. – Muju Oct 25 '16 at 06:06
  • You can simply add any custom UIView on UINavigationBar as title(it comes in middle) using self.navigationItem.titleView = // your UILabel/UIView/UIImageView/UIButton; – KrishnaCA Oct 25 '16 at 06:10
  • Can you send your code with image? – user3182143 Oct 25 '16 at 07:09
  • @user3182143 you can see the image code up in the code. – Muju Oct 25 '16 at 07:11

1 Answers1

1

try like,

   UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)]; // desired frame

[label setFont: [UIFont fontWithName:@"FontAwesome" size:25]]; //desired fornt


UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButton4Frame = listButton4.frame;
listButton4Frame.size = listImage4.size;
listButton4.frame = listButton4Frame;

[listButton4 setTitle:label.text forState:UIControlStateNormal];
//  [listButton4 setImage:listImage4 forState:UIControlStateNormal];
[listButton4 addTarget:self
                action:@selector(ActualNotificationClick:)
      forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton4 =
[[UIBarButtonItem alloc] initWithCustomView:listButton4];

Update :

Try to set attributed string like,

 UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"];


UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButton4Frame = listButton4.frame;
listButton4Frame.size = listImage4.size;
listButton4.frame = listButton4Frame;

UIFont *font = [UIFont fontWithName:@"FontAwesome" size:25.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                                            forKey:NSFontAttributeName];
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"yourTitleString" attributes:attrsDictionary];





[listButton4 setAttributedTitle:attributedStr forState:UIControlStateNormal];


[listButton4 addTarget:self
                action:@selector(ActualNotificationClick:)
      forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton4 =
[[UIBarButtonItem alloc] initWithCustomView:listButton4];

Update 2

  UIFont *font = [UIFont fontWithName:@"FontAwesome" size:25.0];
UIColor *color = [UIColor whiteColor];

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                                            forKey:NSFontAttributeName];
[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];

NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"yourTitleString" attributes:attrsDictionary];
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • newjoinlbl.font = [UIFont fontWithName:@"FontAwesome" size:8]; newjoinlbl.textColor = [UIColor whiteColor]; newjoinlbl.text = [NSString awesomeIcon:FaChild]; [listButton2 setTitle:newjoinlbl.text forState:UIControlStateNormal]; I have added some code Like this – Muju Oct 25 '16 at 06:18
  • I just add @"Your String" to lab.text and it work Thank you Very Much. – Muju Oct 25 '16 at 07:07
  • But I am not able to change the font colour which I have done like before lab.textColor = [UIColor whiteColor]; – Muju Oct 25 '16 at 07:10
  • set color attributes to string not to label!!!! for color you can set `NSForegroundColorAttributeName` ! – Ketan Parmar Oct 25 '16 at 07:15
  • Where to add this? – Muju Oct 25 '16 at 07:18
  • It still in a black colour. – Muju Oct 25 '16 at 07:23