0

我已经编写了这段代码来为 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];

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

4

3 回答 3

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]

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

于 2013-06-18T15:24:52.140 回答
1

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

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() ,或者如果你是子类化的话,作为一个单独的函数。

于 2015-12-02T14:47:45.243 回答
0

Change

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

to

[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected];
于 2014-04-04T04:06:25.280 回答