2016-10-25 55 views
0

我是iOS新手。而且我正面臨着在UINavigation Bar上添加字體真棒的問題。我使用的代碼,圖片喜歡這張如何添加字體真棒在UINavigation欄中的Objective C

UIImage *listImage2 = [UIImage imageNamed:@"Image1.png"]; 
    UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect listButton2Frame = listButton2.frame; 
    listButton2Frame.size = listImage2.size; 
    listButton2.frame = listButton2Frame; 

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


    UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"]; 
    UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom]; 

    // get the image size and apply it to the button frame 
    CGRect listButton4Frame = listButton4.frame; 
    listButton4Frame.size = listImage4.size; 
    listButton4.frame = listButton4Frame; 

    [listButton4 setImage:listImage4 forState:UIControlStateNormal]; 
    [listButton4 addTarget:self 
        action:@selector(ActualNotificationClick:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *jobsButton4 = 
    [[UIBarButtonItem alloc] initWithCustomView:listButton4]; 

    UIImage *listImage3 = [UIImage imageNamed:@"Image3.png"]; 
    UIButton *listButton3 = [UIButton buttonWithType:UIButtonTypeCustom]; 

    // get the image size and apply it to the button frame 
    CGRect listButton3Frame = listButton3.frame; 
    listButton3Frame.size = listImage3.size; 
    listButton3.frame = listButton3Frame; 

    [listButton3 setImage:listImage3 forState:UIControlStateNormal]; 
    [listButton3 addTarget:self 
        action:@selector(EmployeeClick:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *jobsButton3 = 
    [[UIBarButtonItem alloc] initWithCustomView:listButton3]; 

    self.navigationItem.rightBarButtonItems= [NSArray arrayWithObjects:jobsButton2,jobsButton4, nil]; 
    self.navigationItem.leftBarButtonItems=[NSArray arrayWithObjects:jobsButton3, nil]; 

和字體真棒,我使用這樣的

lab.font = [UIFont fontWithName:@"FontAwesome" size:25]; 
lab.textColor = [UIColor whiteColor]; 
lab.text = [NSString awesomeIcon:FaCalendarChecko]; 

我需要添加字體真棒標籤,而不是像上UINavigation條碼。 我使用的圖片是這個樣子 enter image description here

我有添加字體真棒這樣的代碼

newjoinlbl.font = [UIFont fontWithName:@"FontAwesome" size:8]; 
    newjoinlbl.textColor = [UIColor whiteColor]; 
    newjoinlbl.text = [NSString awesomeIcon:FaChild]; 
    UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 

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

    [listButton2 setTitle:newjoinlbl.text forState:UIControlStateNormal]; 
    [listButton2 addTarget:self 
        action:@selector(LogoutClick:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *jobsButton2 = 
    [[UIBarButtonItem alloc] initWithCustomView:listButton2]; 

,但它給我出來把這樣

enter image description here

字體真棒在視野範圍內工作 enter image description here

我只需要添加字體真棒標籤,而不是UINAvigation Bar上的圖像。提前致謝!

+0

你可以隱藏你的導航欄,然後你可以創建自己的導航欄使用視圖和標籤和按鈕的視圖控制器,你需要做以上的實現。 – Wolverine

+0

@Wolverine但這不像UINavigation Bar。 – Muju

+0

NavigationBar用於顯示標題和按鈕。如果你需要這個級別的定製,我建議創建你自己的視圖,看起來像你想要的輸出。我不是說要刪除導航,我說隱藏導航條,然後顯示您自己的觀點。 – Wolverine

回答

1

嘗試像,

UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"]; 

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)]; // desired frame 

[label setFont: [UIFont fontWithName:@"FontAwesome" size:25]]; //desired fornt 


UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom]; 

// get the image size and apply it to the button frame 
CGRect listButton4Frame = listButton4.frame; 
listButton4Frame.size = listImage4.size; 
listButton4.frame = listButton4Frame; 

[listButton4 setTitle:label.text forState:UIControlStateNormal]; 
// [listButton4 setImage:listImage4 forState:UIControlStateNormal]; 
[listButton4 addTarget:self 
       action:@selector(ActualNotificationClick:) 
     forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *jobsButton4 = 
[[UIBarButtonItem alloc] initWithCustomView:listButton4]; 

更新:

嘗試設置attributed string一樣,

UIImage *listImage4 = [UIImage imageNamed:@"Image2.png"]; 


UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom]; 

// get the image size and apply it to the button frame 
CGRect listButton4Frame = listButton4.frame; 
listButton4Frame.size = listImage4.size; 
listButton4.frame = listButton4Frame; 

UIFont *font = [UIFont fontWithName:@"FontAwesome" size:25.0]; 
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font 
                  forKey:NSFontAttributeName]; 
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"yourTitleString" attributes:attrsDictionary]; 





[listButton4 setAttributedTitle:attributedStr forState:UIControlStateNormal]; 


[listButton4 addTarget:self 
       action:@selector(ActualNotificationClick:) 
     forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *jobsButton4 = 
[[UIBarButtonItem alloc] initWithCustomView:listButton4]; 

更新2

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

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

NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"yourTitleString" attributes:attrsDictionary]; 
+0

newjoinlbl.font = [UIFont fontWithName:@「FontAwesome」size:8]; newjoinlbl.textColor = [UIColor whiteColor]; newjoinlbl.text = [NSString awesomeIcon:FaChild]; [listButton2 setTitle:newjoinlbl。text forState:UIControlStateNormal];我已經添加了一些代碼像這樣 – Muju

+0

請參閱更新後的問題 – Muju

+0

您的代碼會在更新問題中給出輸出結果。 – Muju