2016-07-22 45 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謝謝..很高興聽到您的問題已解決。 –