根据控件宽度缩放

根据控件宽度缩放

本文介绍了根据控件宽度缩放 UISegmentedControl 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我找不到任何方法来做到这一点.基本上,我拥有的是一个 UISegmentedControl,带有两个使用 NSLocalizedString 的本地化标签.我已经设置了字体大小,一切在英语和其他几种语言中看起来都很棒.但是,在日语和其他语言中,字符较大并导致标签被截断.

This seems like a no brainer, but I cannot find any way to do this. Basically what I have is a UISegmentedControl with two localized labels using NSLocalizedString. I have set the font size and everything looks great in English and a few other languages. But, in Japanese and other languages the characters are larger and cause the label to be truncated.

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 属性.

Is there any way to scale the font size of the label depending on the label width? I realize that these are not normal UILabel's, so there is no adjustsFontSizeToFitWidth property.

推荐答案

我发现 NSAttributedString 有一个 size 属性,这让我知道如何宽文本适用于每个本地.所以,我可以这样做:

I found out that NSAttributedString has a size property to it, that will allow me to know how wide the text is for every local. So, I can just do something like:

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

这篇关于根据控件宽度缩放 UISegmentedControl 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:27