2016-10-25 74 views
0

我新的iOS.And我面對關於改變字體真棒label.My代碼顏色的一個問題是這樣的如何改變字體顏色真棒上UINavigation酒吧目標C

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 但它在背面顏色,但我寫白色colour.How代碼改變它的顏色。感謝Advace!

+0

您是否確定'FontAwesome'在您的應用中可用?另外,你是否試圖在圖像上設置字體?你確定? – dirtydanee

+0

@dirtydanee字體真棒是在我的應用程序,我設置UILabel上的字體真棒 – Muju

+0

你想改變'listButton2'的背景顏色?你想它是黑色的? – dirtydanee

回答

2

attrsDictionary分配給第二個NSDictionary,而不是第一個。

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

// using literals 

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

你只設置標籤的字體屬性與第一NSDictionary

+0

非常感謝你的代碼運行,只是我需要。 – Muju

+1

沒問題。你做了這段代碼,但是把錯誤的值賦給了'attrsDictionary'。下次更加小心! ;) – dirtydanee

+0

雅肯定。謝謝。 – Muju