Lio*_*ion 7 objective-c uisegmentedcontrol uilabel ios
有没有办法减少适合单段的字体大小UISegmentedControl?
尝试了很多类似的东西,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth];
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5];
Run Code Online (Sandbox Code Playgroud)
和
NSArray *arr = segment.subviews; // segment is UISegmentedControl object
for (int i = 0; i < arr.count; i++) {
UIView *aSegment = [arr objectAtIndex:i];
for (UILabel *label in aSegment.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *myLabel = (UILabel *)label;
[myLabel setNumberOfLines:0];
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
}
}
}
Run Code Online (Sandbox Code Playgroud)
能够设置段标签的行数,如,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
Run Code Online (Sandbox Code Playgroud)
可以根据内容设置单段大小,如,
segment.apportionsSegmentWidthsByContent = YES;
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,每个细分市场的规模都不同
我想保持每一段相同的大小和要减少字体大小可以适合于UISegmentLabel (label)的UISegmentedControl类似minimumscalefactor或minimumfontsize或adjustsFontSizeToFitWidth.包含in时,这些属性不适用于标签UISegmentedControl.
如果任何人可以帮助实现这一点,将不胜感激!
提前致谢!!
我找到了问题,其实这是我的错!我正在设定numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactor并且为此而努力TitleTextAttributes.如果我们设置titleTextAttribute则minimumScaleFactor无法工作.
更新:(正如@ HawkEye1194在另一个答案的评论中所提出的那样)
我最终得到了以下解决方案,
//this will allow multiple lines in label contained by every segment in segmentedcontroller
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:option];
segment.frame = CGRectMake(20, 50, self.view.frame.size.width - 40, 50);
segment.tintColor = [UIColor grayColor];
segment.selectedSegmentIndex = 0;
segment.backgroundColor = [UIColor whiteColor];
segment.tag = segmentedControllerBaseTag;
[segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[segment setTitleTextAttributes:@{NSFontAttributeName :[UIFont fontWithName:@"HelveticaNeue" size:17.0], NSForegroundColorAttributeName : [UIColor darkGrayColor] } forState:UIControlStateNormal];
[segment setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:17.0],NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
如果你没有设置如上所述的标题textattribute,那么你可以使用下面的代码
// ********** if titletextattributes are not set then below method works ***********
for(uint i=0;i<[segment subviews].count;i++)
{
for(UIView *view in [[[segment subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
[(UILabel*)view setNumberOfLines:0];
[(UILabel*)view setAdjustsFontSizeToFitWidth:YES];
[(UILabel*)view setMinimumScaleFactor:0.7];
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下代码根据内容调整单个细分受众群的大小,
//*************** adjust single segment size as per content
segment.apportionsSegmentWidthsByContent = YES;
Run Code Online (Sandbox Code Playgroud)