gpt4 book ai didi

ios - 如何在 Objective c 的 UI 导航栏上更改 Font Awesome 的颜色

转载 作者:行者123 更新时间:2023-11-28 21:19:31 25 4
gpt4 key购买 nike

我是 iOS 的新手。我遇到了一个关于更改 Font Awesome 标签颜色的问题。我的代码是这样的

    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但它是背景颜色,但我写了一个白色代码。如何改变它的颜色。提前致谢!

最佳答案

attrsDictionary 分配给第二个 NSDictionary,而不是第一个。

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

// using literals

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

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

关于ios - 如何在 Objective c 的 UI 导航栏上更改 Font Awesome 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40236348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com