如何更改UISegmentedControl上文本的字体大小?
问题描述:
以下是初始化我的代码UISegmentedControl
。如何更改UISegmentedControl上文本的字体大小?
- (void)initializeToolButtons
{
NSArray *buttonTitles = [NSArray arrayWithObjects:@"ANNEXET", @"HOVET", @"GLOBEN", "ALL", nil];
toolbuttons = [[UISegmentedControl alloc] initWithItems:buttonTitles];
toolbuttons.segmentedControlStyle = UISegmentedControlStyleBar;
toolbuttons.tintColor = [UIColor darkGrayColor];
toolbuttons.backgroundColor = [UIColor blackColor];
toolbuttons.frame = CGRectMake(0, 0, 320, 30);
[toolbuttons addTarget:self action:@selector(toolButtonsAction) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:toolbuttons];
}
我怎样才能减少字体大小在UISegmentedControl
每个项目?
注:toolButtons
已经全局声明。
答
考虑重新设计自己的界面,或者使用“标签”的风格,有一个较小的字体。混淆未公开的属性可能会让你的应用程序被拒绝,或者如果他们在引擎盖下改变了某些东西,就会破坏你的应用程序
例如,给出的代码示例不起作用。当您点击某个段时,该段的字体会重置为正常大小。如果你做的事情偏离这些事情的正常使用,任何不可预知的事情都可能在你的应用中发生或改变。所以,如果你想要一个应用程序,将继续在以下操作系统更新坚持标准的东西,或使用UIButtons和矩形背景图像自己的控件。黑客现在可能会工作,但不会说它将来会发生。
答
很简单:
UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont];
[segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
这适用于iOS 5及以上版本。 – adbie 2012-11-22 06:57:44
在iOS 7.0中使用NSFontAttributeName – Stavash 2014-01-06 14:59:34