如何调整文字(字体)以适应UISegmentedControl的UISegment?

有没有什么办法可以缩小UISegmentedControl单个部分的字体大小?

已经尝试了很多东西,

 [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth]; [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5]; 

  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; } } } 

可以设置段的标签行数,

  [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0]; 

可以根据内容设置单个分段大小,

  segment.apportionsSegmentWidthsByContent = YES; 

但在这种情况下,每个细分都有不同的尺寸。

我想要保持每个段的大小相同,并且想要缩小UISegmentLabel (label)的字体大小,例如minimumscalefactorminimumfontsizeadjustsFontSizeToFitWidth 。 当包含在UISegmentedControl时,这些属性不能用于标签。

如果有人可以帮助实现这一点,将不胜感激!

提前致谢!!

我发现这个问题,其实这是我的错误! 我设置了numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactorTitleTextAttributes toghether。 如果我们设置titleTextAttribute那么minimumScaleFactor不能工作。

更新:(由@ HawkEye1194在另一个答案的评论中提出)

我最终以下面的解决scheme,

  //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]; 

如果你没有像上面那样设置标题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]; } } } 

您可以根据以下代码调整单个细分的尺寸,

  //*************** adjust single segment size as per content segment.apportionsSegmentWidthsByContent = YES; 

试试这个,我希望这会帮助你,你会明白这是如何工作的 –

我有一个UISegmentedControl即有三个段的_userProfileSagmentOutlet 。 这里是示例代码 –

 CGFloat fontSize = 15; [_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected]; [_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal]; 

这是以前的代码截断标题的尾巴像下面的图像 –

在这里输入图像说明

这里是适合每个标题在相同的字体大小段的主要逻辑 –

 CGFloat fontSize = 15; NSAttributedString* firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; NSAttributedString* secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; NSAttributedString* thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; float maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width); while (maxW > _userProfileSagmentOutlet.subviews[0].frame.size.width) { fontSize--; firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}]; maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width); } [_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected]; [_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal]; 

使用此代码后,图像看起来像这样(相同的字体大小和文本适合分割和工作正常) –

在这里输入图像说明

如果有人需要,这里是Swift扩展,

  var fontSize:CGFloat = 15.0; var firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]) var secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]); var thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]); var maxW:CGFloat = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width); while (maxW > userProfileSagmentOutlet.subviews[0].frame.size.width) { fontSize--; firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]) secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]); thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]); maxW = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width); } userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal) userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)