如何在Objective c中更改UINavigation Bar上Font Awesome的颜色

时间:2016-10-25 09:32:15

标签: ios objective-c xcode uinavigationbar

我是iOS新手。我正面临着改变字体真棒标签颜色的问题。我的代码是这样的

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

输出看起来像这样 enter image description here 但它背面颜色,但我写了一个白色的代码。如何改变它的颜色。感谢Advace!

1 个答案:

答案 0 :(得分:2)

attrsDictionary分配给第二个NSDictionary,而不是第一个。{/ p>

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

// using literals

NSDictionary *attrsDictionary = @{NSFontAttributeName:font,NSForegroundColorAttributeName:color}; //

您只使用第一个NSDictionary设置标签的字体属性。