我是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];
答案 0 :(得分:2)
将attrsDictionary
分配给第二个NSDictionary
,而不是第一个。{/ p>
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
或
// using literals
NSDictionary *attrsDictionary = @{NSFontAttributeName:font,NSForegroundColorAttributeName:color}; //
您只使用第一个NSDictionary
设置标签的字体属性。