【问题标题】:How to add font awesome On UINavigation Bar in Objective C如何在Objective C的导航栏上添加字体真棒
【发布时间】:2016-10-25 05:49:17
【问题描述】:

我是 iOS 新手。我在 UINavigation Bar 上添加很棒的字体时遇到了问题。我正在使用像这样的图像代码

  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 Bar 上添加字体 awesome Label 而不是图像。 我使用的图像看起来像这样

我有像这段代码一样添加字体真棒

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

但它给了我这样的输出

字体真棒在视图中正常工作

我只需要在 UIAvigation Bar 上添加字体真棒标签而不是图像。提前致谢!

【问题讨论】:

  • 您可以隐藏您的导航栏,然后您可以使用视图和标签以及您需要在其中执行上述实现的视图控制器的按钮创建自己的导航栏。
  • @Wolverine 但这与 UINavigation Bar 不同。
  • NavigationBar 用于显示标题和按钮。如果您需要这种级别的自定义,我建议您创建自己的视图,该视图看起来像您想要的输出。我不是说要删除导航,我是说隐藏 NavigatioBar 然后显示你自己的视图。

标签: ios objective-c xcode uinavigationbar font-awesome-3.2


【解决方案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];

【讨论】:

  • newjoinlbl.font = [UIFont fontWithName:@"FontAwesome" size:8]; newjoinlbl.textColor = [UIColor whiteColor]; newjoinlbl.text = [NSString awesomeIcon:FaChild]; [listButton2 setTitle:newjoinlbl.text forState:UIControlStateNormal];我已经添加了一些这样的代码
  • 我只是将@"Your String" 添加到lab.text,它就可以工作了,非常感谢。
  • 但我无法像之前那样更改字体颜色 lab.textColor = [UIColor whiteColor];
  • 将颜色属性设置为字符串而不是标签!!!!对于颜色,您可以设置NSForegroundColorAttributeName
  • 在哪里添加这个?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
相关资源
最近更新 更多