【问题标题】:Is it possible to have different style font when on a selected tab in UISegmentedControl?在 UISegmentedControl 中的选定选项卡上是否可以使用不同的样式字体?
【发布时间】:2013-06-18 15:00:11
【问题描述】:

我已经编写了这段代码来为 UISegmentedControl 设置正常状态和突出显示状态的字体属性,但它不起作用。请帮我指出原因。

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica" size:12.0f], UITextAttributeFont,

                            [UIColor blackColor], UITextAttributeTextColor,

                            nil];

NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:12.0f], UITextAttributeFont,

                            [UIColor blackColor], UITextAttributeTextColor,

                            nil];



[self.tab setTitleTextAttributes:attributes forState:UIControlStateNormal];

[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateHighlighted];

我希望选定的文本是粗体,否则应该是正常的。

【问题讨论】:

  • 我只改变了文本颜色,设置字体没有做任何改变
  • 你试过 UIControlStateSelected 了吗?
  • 不,我没有尝试,现在我又添加了一行:[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected];
  • 为什么这被标记为iOS4? iOS 5.0 增加了UISegmentedControlsetTitleTextAttributes:forState: 方法。

标签: iphone ios objective-c ios4


【解决方案1】:

我已经尝试过如下

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                        [UIFont boldSystemFontOfSize:17], UITextAttributeFont,
                        [UIColor blackColor], UITextAttributeTextColor,
                        nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor    whiteColor] forKey:UITextAttributeTextColor];
[_segmentedControl setTitleTextAttributes:highlightedAttributes  forState:UIControlStateHighlighted];

并设置字体

 [UIFont fontWithName:@"FontName" size:desiredsize.f]

还将突出显示更改为选中。

【讨论】:

  • 我根据需要将不同选项卡的着色颜色更改为较浅的蓝色和绿色阴影,因此我无法将其更改为白色,因为那时它看不到。查看是否为粗体的一种方法是,由于某种原因,我的代码无法正常工作。
  • 我也只是尝试将突出显示更改为选定的相同结果
  • 您可以添加自己的色调,而不是我的代码中的颜色。对于使用粗体,只需在字体名称的末尾加上 - 即可。它通常对我有用,但请记住 for 必须是 uifont 家族的一部分。
  • 是的,我为 UIControlStateHighlighted 和 UIControlStateSelected 输入了两个代码,并给了它们加粗字体,但仍然无法正常工作。为了安全起见,我使用了 systemFont 和 boldSystemFont 来确保我没有添加不存在的字体。
  • 尝试将 self.tab 更改为段控制器。那可能是你的问题。
【解决方案2】:

这个答案已经过时了,但是对于那些正在寻找快速解决方案的人,您可能想尝试这种方法:

func stylizeFonts(){
    let normalFont = UIFont(name: "Helvetica", size: 16.0)
    let boldFont = UIFont(name: "Helvetica-Bold", size: 16.0)

    let normalTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName: normalFont!
    ]

    let boldTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : boldFont!,
    ]

    self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
    self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted)
    self.setTitleTextAttributes(boldTextAttributes, forState: .Selected)
}

请务必在您的 viewDidLoad 中添加 stylizeFonts() ,如果您是子类化,请务必将其添加为单独的函数。

【讨论】:

    【解决方案3】:

    改变

    [self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateHighlighted];
    

    [self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2018-09-24
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多