4

이것은 생각할 줄 모르는 것처럼 보이지만이를 수행 할 방법을 찾을 수 없습니다. 기본적으로 내가 갖고있는 것은 NSLocalizedString을 사용하는 두 개의 현지화 된 레이블이있는 UISegmentedControl입니다. 글꼴 크기를 설정했는데 영어와 다른 언어로 된 모든 것이 멋지게 보입니다. 그러나 일본어와 다른 언어에서는 문자가 더 크고 레이블이 잘 리게됩니다.스케일 UISegmentedControl 컨트롤의 너비를 기준으로 한 레이블

enter image description here

self.segmentedControl = [[UISegmentedControl alloc] initWithItems: 
          [NSArray arrayWithObjects: 
           NSLocalizedString(@"Miles", nil).uppercaseString, 
           NSLocalizedString(@"Kilometers", nil).uppercaseString, 
           nil]]; 
self.segmentedControl.apportionsSegmentWidthsByContent = YES; 
self.segmentedControl.selectedSegmentIndex = self.metric ? 1 : 0; 
[self.segmentedControl addTarget:self action:@selector(changeMetric:) forControlEvents:UIControlEventValueChanged]; 
self.segmentedControl.frame = CGRectMake(8, middleHolder.frame.size.height/2+60, progressWidth, 30); 
self.segmentedControl.center = CGPointMake(self.view.center.x, self.segmentedControl.center.y); 
[self.segmentedControl setTitleTextAttributes:@{ 
                NSForegroundColorAttributeName: [UIColor whiteColor], 
                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:36] 
                } forState:UIControlStateSelected]; 
[self.segmentedControl setTitleTextAttributes:@{ 
                NSForegroundColorAttributeName: [[UIColor whiteColor] colorWithAlphaComponent:0.3], 
                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:36] 
                } forState:UIControlStateNormal]; 
self.segmentedControl.tintColor = [UIColor clearColor]; 
self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
[middleHolder addSubview:self.segmentedControl]; 

라벨 폭에 따라 라벨의 글꼴 크기를 확장 할 수있는 방법이 있습니까? 나는 이것이 정상이 아니란 것을 깨닫는다 UILabel, 그래서 아무 adjustsFontSizeToFitWidth 재산이 없다.

답변

0

나는 NSAttributedStringsize 속성이 있다는 것을 알았습니다. 그 텍스트는 모든 지역의 텍스트가 얼마나 넓은 지 알 수 있습니다. 따라서 다음과 같이 할 수 있습니다.

CGFloat fontSize = 36; 
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Kilometers", nil).uppercaseString attributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:fontSize]}]; 
if (attributedString.size.width > 200) { 
    fontSize = 30; 
} 
1

나는 동일한 문제를 다루고 있습니다. 여기에 내 솔루션 (가장자리가 약간 거친) :

세그먼트 화 된 컨트롤을 서브 클래스 화 한 다음 내 서브 클래스에서 다음을 수행하십시오. 희망이 도움이! (iOS 7 및 8에서 테스트 됨)

+ (void)setSublabelScale:(UIView *)view { 
    for (id subview in view.subviews) { 
     if ([subview isKindOfClass:[UILabel class]]) { 
      [subview setMinimumScaleFactor:0.5]; 
      [subview setAdjustsFontSizeToFitWidth:YES]; 
     } else { 
      [MYSegmentedControl setSublabelScale:subview]; 
     } 
    } 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    [MYSegmentedControl setSublabelScale:self]; 
} 
관련 문제