简体   繁体   English

调整UILabel字体大小以适应设备尺寸

[英]Adjust UILabel font size for device dimension

Is there a way to set UILabel font to be greater in big device and smaller in small device? 有没有一种方法可以将UILabel字体设置为在大设备中较大而在小设备中较小?

Have I to do it programmatically, checking every time the device size? 我是否需要以编程方式进行操作,每次检查设备大小时都进行检查?

My approach is this, now: 现在,我的方法是这样的:

class func hightlightPtSize(height: CGFloat) -> CGFloat {
    return height / 25
}

class func notSoHighlightPtSize(height: CGFloat) -> CGFloat {
    return height / 30
}

class func stdPtSize(height: CGFloat) -> CGFloat {
    return height / 35
}

class func sizeForType(height: CGFloat, type: Int) -> CGFloat {
    switch type {
    case STD:
        return stdPtSize(height: height)
    case HIGHLIGHT:
        return hightlightPtSize(height: height)
    case NOT_SO_HIGH:
        return notSoHighlightPtSize(height: height)
    default:
        return 0
    }
}

I have solution for your problem. 我有解决您问题的方法。 Below my code in Objective C. 在我的目标C代码下方。

float newFontSize = [UIScreen mainScreen].bounds.size.height * (fontSize / 568.0);
        if ([UIScreen mainScreen].bounds.size.height < 500) {
            newFontSize = [UIScreen mainScreen].bounds.size.height * (fontSize / 480.0);
        }
        self.label.font = [UIFont fontWithName:self.label.font.fontName size:newFontSize];

I hope this helps you. 我希望这可以帮助你。

I tried before by making my default font for all labels larger than any possibility used - 60 in my case. 之前,我尝试过使所有标签的默认字体都大于使用的任何字体-在我的情况下为60。 Then set the adjustsFontSizeToFitWidth property to true to let the system size the font for me, while I set the size of my UILabel . 然后,将adjustsFontSizeToFitWidth属性设置为true,以让系统为我设置字体大小,同时设置UILabelsize

To make a label proportionate to the screen size you can use the autoresizing feature of storyboard. 要使标签与屏幕尺寸成比例,可以使用情节提要的自动调整大小功能。 With the UILabel selected, if there is no existing constraints, you can see something like this: 选择UILabel ,如果不存在现有约束,则可以看到类似以下内容的内容:

在此处输入图片说明

In the right panel of Xcode, click the lines to toggle it on or off, the four line on the sides will tell if the view will maintain it's distance from the sides of it's superview the two in the square will tell if it resizes with it's superview in your case, make sure those are on as in the image above. 在Xcode的右面板中,单击线条以将其打开或关闭,侧面的四条线将告诉您视图是否保持其与superview的距离,而广场中的两条线将告诉您是否调整其尺寸。 superview您的情况而定,请确保如上图所示启用这些功能。 As for the other four that I did not on, you can try experimenting toggling each on or off in combinations with others to find the exact behaviour you want. 至于我没有提到的其他四个,您可以尝试与其他人一起切换开或关,以找到所需的确切行为。

If constraints are needed by it's siblings or super/sub views, I can suggest you try a set of constraints like: 如果兄弟姐妹或超级/子视图需要约束,我建议您尝试一组约束,例如:

  • Label(width) equal to SuperView(width) - change the multiplier to set how much of the space you want to take 标签(宽度)等于SuperView(宽度)-更改乘数以设置要占用的空间
  • Label(height) equal to SuperView(height) - same as width 标签(高度)等于SuperView(高度)-与宽度相同
  • Label(leading) equal to SuperView(leading) - change constant to set distance from leading edge, can change to trailing edge if needed 标签(前导)等于SuperView(前导)-更改常量以设置到前缘的距离,可以根据需要更改为后缘
  • Label(Top) equal to SuperView(Top) same as leading, can change to bottom if needed. Label(Top)等于SuperView(Top),与前导相同,如果需要,可以更改为底部。

It might be slightly verbose, but mainly take note of the first two to get the sizing, set the multiplier of the constraints not constant . 这可能是稍微详细,但主要是注意到前两次拿到大小,设置的约束不恒定乘数 The next two are mainly just to satisfy the requirements to properly place your label in the view. 接下来的两个主要是为了满足将标签正确放置在视图中的要求。 You can freely align centers or align top/bottom/leading/trailing in combinations to position it where you want. 您可以自由对齐中心,也可以组合对齐顶部/底部/前导/尾随以将其放置在所需的位置。

My Swift version of kishan godhani's solution: 我的Kishan Godhani解决方案的Swift版本:

let screen = UIScreen.main
var newFontSize = screen.bounds.size.height * (defaultFont / 568.0);
if (screen.bounds.size.height < 500) {
    newFontSize = screen.bounds.size.height * (defaultFont / 480.0);
}
label.font = label.font.withSize(newFontSize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM