2016-07-22 6 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; 

しかし、この場合、すべてのセグメントのサイズが異なります。

私はすべてのセグメントの同じサイズを維持したいとminimumscalefactorまたはminimumfontsizeまたはadjustsFontSizeToFitWidthようUISegmentLabel (label)UISegmentedControlの何かにフィットすることができ、フォントサイズを小さくしたいです。 UISegmentedControlに含まれている場合、これらのプロパティはラベルに対して機能しません。

これを達成するのに役立つ人がいれば、感謝します!

ありがとうございます!

+1

このhttp://stackoverflow.com/questions/26453297/scale-uisegmentedcontrol-labels-based-on-width-of-controlを見ると、このhttp://stackoverflow.com/questions/22165171/を見ます複数の行のテキスト・ショー・アフター・ザ・セグメント・オブ・ユーズ・コントロール・クリック-bあなたは若干のアイデアを得ます –

+0

@ Anbu.Karthik:リンクありがとう!!実際に私は少し間違いを犯していましたが、今解決しました! – Lion

答えて

6

をneeded-場合、私は実際にそれが私の間違いだった、問題を発見したスウィフト拡張機能です!私はnumberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactorTitleTextAttributesを別に設定していました。 titleTextAttributeを設定した場合、minimumScaleFactorは動作しません。

更新:(別の答えのコメントで@ HawkEye1194で尋ねた通り)

あなたは、あなたがすることができ、上記のように、タイトルのTextAttributeを設定しない場合、私は解決策、

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

以下で終わるています以下のコードを使用してください

以下のコードでコンテンツごとに調整することができます

//*************** adjust single segment size as per content 

segment.apportionsSegmentWidthsByContent = YES; 
3

は、私は、これはあなたを助ける、あなたは、これは私がUISegmentedControlすなわち_userProfileSagmentOutletは三つのセグメントを持つ必要があり

をWORKS-どのようにアイデアを得ることを願って、これを試してみてください。ここで試料は、これがイメージ -

enter image description here

以下のようなここで表題のトランケートテールが同じ有するセグメント内の各タイトルに合うメインロジックで前のコードである

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

セグメントと正常に動作します) -

enter image description here

ここでは、誰かが

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

これは良いアプローチですが、私の場合、セグメント(タイトル)の数が修正されていないので、私の場合は適切ではありません、それはいくつかの時間が5以上の動的な時間です。結構いい。 +1! – Lion

+0

@Lionありがとうございました。あなたの問題が解決されたと聞いて幸いです。 –

関連する問題