我编写了这段代码来为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];
我希望所选文字为粗体,否则应该是正常的。
答案 0 :(得分: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]
同时将突出显示更改为选中。
答案 1 :(得分: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()或在子类化时添加单独的函数。
答案 2 :(得分:0)
更改
[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateHighlighted];
到
[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected];