簡體   English   中英

iOS:在UIBarButtonItem和Button中添加帶有標題的FontAwesome圖標

[英]iOS: Append FontAwesome icon with title in UIBarButtonItem and Button

我想在我的NavigationBar中使用這樣的UIBarButton項目

在此輸入圖像描述

像這樣的普通UIButton

在此輸入圖像描述

我已經嘗試過使用這些鏈接問題1 問題2 問題3但沒有得到輸出。

我需要在FontAwesome圖標上添加按鈕標題文本。

任何人都可以幫助嗎?

我的代碼是:

NSString *icon = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-folder-open"];

NSString *locationString = [NSString stringWithFormat:@"%@ %@", icon, @"Change"];

NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:locationString];

[astring addAttribute:NSFontAttributeName
                value:[UIFont iconicFontOfSize:20]
                range:NSMakeRange(0,1)]; // The first character

changeFolderButton.titleLabel.attributedText = astring;

如果FontAwesome Icon的代碼是"&#xf007" ,則將文本設置為@"\\U0000f007"為Title。

使用以下代碼為UIButton : -

myBtn.titleLabel.font =[UIFont fontWithName:@"FontAwesome" size:20.0];
[myBtn setTitle:@"\U0000f007" forState:UIControlStateNormal]];

使用以下代碼為UIBarButton : -

[self.barButton setTitleTextAttributes:@{
              NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
              NSForegroundColorAttributeName: self.view.tintColor
                                     } forState:UIControlStateNormal]; 
[self.barButton setTitle:@"\U0000f007"]];

如果要附加帶有圖標的字符串,請使用以下代碼: -

NSString * myString = @"\U0000f007 Change";
NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:myString];
[astring addAttribute:NSFontAttributeName
                value:[UIFont fontWithName:@"FontAwesome" size:22.0]
                range:NSMakeRange(0,1)]; //If Icon is on starting position
[myBtn setAttributedTitle:astring forState:UIControlStateNormal];

希望,這是你正在尋找的。 任何關注都會回復給我。 :)

在swift 2.0中:

    btnAddGroup.titleLabel?.font = UIFont(name:"FontAwesome",size: 50)
    btnAddGroup.setTitle(String.fontAwesomeIconWithCode("fa-plane"), forState: .Normal)

對不起,遲到的答案,但這可能會幫助將來的人,注意:Facebook表示我的UIButton

FAKFontAwesome *Gplus = [FAKFontAwesome googleIconWithSize:20];
    [Gplus addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
    NSMutableAttributedString *twitterMass = [[Gplus attributedString] mutableCopy];
    [twitterMass appendAttributedString:[[NSAttributedString alloc] initWithString:@" Login With Google" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]];
    [Facebook setAttributedTitle:twitterMass forState:UIControlStateNormal];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM