iOS:在UIBarButtonItem和Button中添加带有标题的FontAwesome图标 [英] iOS: Append FontAwesome icon with title in UIBarButtonItem and Button

查看:250
本文介绍了iOS:在UIBarButtonItem和Button中添加带有标题的FontAwesome图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的NavigationBar中使用这样的UIBarButton项目和





这样的普通UIButton





我已经尝试过使用这些链接
问题1 问题2 问题3 但没有得到输出。



我需要在FontAwesome图标上添加按钮标题文本。



有人可以提供帮助吗?



我的代码是:

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

NSString * locationString = [NSString stringWithFormat:@%@%@,图标,@更改];

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

[astring addAttribute:NSFontAttributeName
value:[UIFont iconicFontOfSize:20]
range:NSMakeRange(0,1)]; //第一个字符

changeFolderButton.titleLabel.attributedText = astring;


解决方案

如果代码 FontAwesome 图标是& #xf007,然后将文字设置为 @\ U0000f007进入标题。



使用以下代码 UIButton : -

  myBtn.titleLabel.font = [UIFont fontWithName:@FontAwesomesize:20.0]; 
[myBtn setTitle:@\ U0000f007forState:UIControlStateNormal]];

使用以下代码 UIBarButton : -



'pre> [self.barButton setTitleTextAttributes:@ {
NSFontAttributeName:[UIFont fontWithName:@ FontAwesome 尺寸: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
值为:[UIFont fontWithName:@ FontAwesome 尺寸:22.0
范围:NSMakeRange(0,1)]。 //如果Icon位于起始位置
[myBtn setAttributedTitle:astring forState:UIControlStateNormal];

希望,这就是你要找的东西。任何关注都会回复给我。 :)


I want a UIBarButton Item like this in my NavigationBar and

Normal UIButton like this

I already tried using these links Question 1 Question 2 Question 3 but didn't got the output.

My need is to append that FontAwesome icon with the button title text.

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.

Use Following code For UIButton:-

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

Use Following code For 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. :)

这篇关于iOS:在UIBarButtonItem和Button中添加带有标题的FontAwesome图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆