gpt4 book ai didi

objective-c - 如何更改分段控件的字体大小并防止其在更改段后变回默认大小

转载 作者:可可西里 更新时间:2023-11-01 05:06:44 26 4
gpt4 key购买 nike

我正在使用以下代码来实现并随后更改 UISegmented 控件中每个段的字体大小

//Set up segment control
UISegmentedControl *tempSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Friends",@"Popular", nil]];
tempSegmentControl.frame = CGRectMake(-8, -1, 336, 30);

self.segmentControl = tempSegmentControl;
[self.segmentControl setWidth:168 forSegmentAtIndex:0];
[self.segmentControl setWidth:168 forSegmentAtIndex:1];
self.segmentControl.selectedSegmentIndex = 0;
[self.segmentControl addTarget:self action:@selector(toggleControls:) forControlEvents:UIControlEventValueChanged];
[self.segmentControl setSegmentedControlStyle:UISegmentedControlStylePlain];

//TO CHANGE FONT SIZE OF EACH SEGMENT
for (id segment in [self.segmentControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:14]];
}
}
}

这最初有效(见下面的截图)

enter image description here

但是,当我点击“常用”选项卡(非事件选项卡)后,字体大小似乎恢复到原来的默认字体大小

enter image description here

如何防止字体大小变回默认大小?

最佳答案

可能不是最干净的方法,但如果您在 UISegmentedControl 控件的“值已更改”事件上运行 for 循环,它会起作用。

更新:这是正确的方法,适用于 iOS 5 及更高版本:

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14], UITextAttributeFont, nil];
[self.segmentControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

关于objective-c - 如何更改分段控件的字体大小并防止其在更改段后变回默认大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com