Muju Muju - 1 month ago 5
Objective-C Question

How to change the colour of Font Awesome on UINavigation Bar in Objective c

I am new in iOS.And I am facing a problem regarding to change the colour of font awesome label.My code is like this

UILabel *lab =[[UILabel alloc] init];
lab.text = [NSString awesomeIcon:FaChild];

UIImage *listImage2 = [UIImage imageNamed:@"Image.png"];
UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
listButton2.backgroundColor=[UIColor whiteColor];
[[listButton2 layer] setBorderWidth:0.5f];
listButton2.layer.borderColor =[[UIColor blackColor] CGColor];
listButton2.layer.cornerRadius = calenderbtn.bounds.size.width / 3.4;// this value vary as per your desire
listButton2.clipsToBounds = YES;

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

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

NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:lab.text attributes:attrsDictionary];



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

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


And it output look like this
enter image description here
But it in back colour but I write a code for white colour.How to change its colour. Thanks in Advace!

Answer

Assign attrsDictionary to the second NSDictionary, not to the first one.

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

You have only set the font property of the label with the first NSDictionary.