2016-07-22 46 views
4

有没有什么办法可以减小字体大小,可以适合UISegmentedControl的单个部分?如何调整文字(字体)以适应UISegmentedControl的UISegment?

已经尝试了许多事情类似,

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

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

我想保持每一段相同的大小和要减少字体大小,可以在UISegmentedControlminimumscalefactorminimumfontsizeadjustsFontSizeToFitWidthUISegmentLabel (label)是合适的。当包含在UISegmentedControl中时,这些属性不适用于标签。

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

在此先感谢!

+1

看到这个http://stackoverflow.com/questions/26453297/scale-uisegmentedcontrol-labels-based-on-width-of-control看这http://stackoverflow.com/questions/22165171/多行的文字显示一段uisegmentedcontrol被点击-B你有一些想法 –

+0

@ Anbu.Karthik:谢谢你的链接!其实我犯了一点小错误,现在解决了! – Lion

回答

6

我发现这个问题,其实这是我的错!我正在设置numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactorTitleTextAttributes。如果我们设置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]; 

如果没有设置标题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; 
3

试试这个,我希望这会帮助你,你会得到一个想法如何这个工程─

我有有三段一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]; 

这是先前码截尾标题的像下面图像 -

enter image description here

这里是适合每个标题中的段与相同的主逻辑字体大小 -

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

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

enter image description here

这是斯威夫特的扩展,如果有人needed-

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) 
+0

这是一个很好的方法,但不适合我的情况,因为在我的情况下,段(标题)的数量不是固定的,它是动态的,有时是2,有时它可以是5或更多等等。但是你的方法是挺好的。 +1! – Lion

+0

@Lion谢谢..很高兴听到您的问题已解决。 –