2016-07-22 3 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 같은 UISegmentedControl 무엇인가 UISegmentLabel (label)에 적합 할 수있는 글꼴 크기를 줄이고 자 할. UISegmentedControl에 포함 된 경우 라벨에 대해 이러한 속성이 작동하지 않습니다.

아무도이를 달성하는 데 도움이 될 수 있다면, 그것을 주시면 감사하겠습니다 !!

미리 감사드립니다.

+1

볼이 http://stackoverflow.com/questions/26453297/scale-uisegmentedcontrol-labels-based-on-width-of-control 볼이 http://stackoverflow.com/questions/22165171/ 여러 줄짜리 텍스트 줄을 보여준 후에, 당신이 어떤 아이디어를 얻었을 때 –

+0

@ Anbu.Karthik : 링크에 감사드립니다! 사실 나는 약간의 실수를 저질렀고 이제 그것을 풀었다. – Lion

답변

6

needed- 경우

enter image description here

여기 스위프트 확장입니다! 나는 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은 3 개 개의 세그먼트를 가진이 아이디어를 얻을 것입니다. 다음은 샘플 코드 -

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

이 코드 이미지를 사용한 후 사용 후 (같은 글꼴 크기 및 텍스트에 맞음) 세그먼트는) 잘 작동 - 누군가가 내가 실제로 내 실수, 문제를 발견

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 감사합니다. 문제가 해결되었다는 소식을 듣고 기쁘게 생각합니다. –

관련 문제