简体   繁体   English

iOS:在UIBarButtonItem和Button中添加带有标题的FontAwesome图标

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

I want a UIBarButton Item like this in my NavigationBar and 我想在我的NavigationBar中使用这样的UIBarButton项目

在此输入图像描述

Normal UIButton like this 像这样的普通UIButton

在此输入图像描述

I already tried using these links Question 1 Question 2 Question 3 but didn't got the output. 我已经尝试过使用这些链接问题1 问题2 问题3但没有得到输出。

My need is to append that FontAwesome icon with the button title text. 我需要在FontAwesome图标上添加按钮标题文本。

Can anyone help in this? 任何人都可以帮助吗?

My code is: 我的代码是:

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;

If code of FontAwesome Icon is "&#xf007" , then set text to @"\\U0000f007" into Title. 如果FontAwesome Icon的代码是"&#xf007" ,则将文本设置为@"\\U0000f007"为Title。

Use Following code For UIButton :- 使用以下代码为UIButton : -

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

Use Following code For UIBarButton :- 使用以下代码为UIBarButton : -

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

If you want to append your string with icon then use following code:- 如果要附加带有图标的字符串,请使用以下代码: -

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];

Hope, this is what you're looking for. 希望,这是你正在寻找的。 Any concern get back to me. 任何关注都会回复给我。 :) :)

In swift 2.0: 在swift 2.0中:

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

Sorry for late answer but this might help someone in future,Note:Facebook denotes my UIButton 对不起,迟到的答案,但这可能会帮助将来的人,注意: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