ios 按屏幕大小缩放文本标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29308941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:23:55  来源:igfitidea点击:

Scale text label by screen size

iosxcodeswift

提问by Jeffrey

Is there a way to scale text so that it takes up close to the same screen real estate no matter what the device size is? I've found that the text on an iPad sized device is too small in relation to the screen size when compared to the iPhone. Below is an example of what I'm looking for. Notice the text percentage size is similar in relation to the device screen size.

有没有办法缩放文本,使其无论设备大小如何都接近相同的屏幕空间?我发现与 iPhone 相比,iPad 大小的设备上的文本相对于屏幕尺寸来说太小了。下面是我正在寻找的一个例子。请注意,文本百分比大小与设备屏幕大小相似。

Example

例子

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by William Dayanayev

To set constraints on the label that you have, see this link: How do you make a background image scale to screen size in swift?. I know that you might not be using Swift (I'm using Objective-C), but the first answer shows how to do it in the storyboard. Do the same thing it says, but for the label. Then, see the image below to change the auto shrink options for the label from "Fixed Font Size" to "Minimum Font Scale" (see image below). Hope this helps!

要对您拥有的标签设置约束,请参阅此链接:如何快速将背景图像缩放到屏幕大小?. 我知道您可能没有使用 Swift(我使用的是 Objective-C),但第一个答案显示了如何在故事板中进行操作。做同样的事情,但对于标签。然后,参见下图,将标签的自动缩小选项从“固定字体大小”更改为“最小字体比例”(见下图)。希望这可以帮助!

Changing Font Size

更改字体大小

回答by bearacuda13

I had my own fix and it's not one click, but I found it well worth it.

我有自己的修复方法,不是一键解决,但我发现它非常值得。

Step 1:

第1步:

Test some font sizesfor different screen sizes, recordingthe font sizeand the most relevant other dimension.

针对不同的屏幕尺寸测试一些字体大小记录字体大小和最相关的其他维度

Here's what I mean...

这就是我的意思...

I'd start with a normal iPad Screen and select a font size that worked.

我会从一个普通的 iPad 屏幕开始,然后选择一个有效的字体大小。

enter image description here

在此处输入图片说明

Since I knew the entire height of the screen would determine what makes a font size comfortable or not, I'd record the font size along with the entire screen height.

由于我知道屏幕的整个高度将决定字体大小是否舒适,因此我会记录字体大小以及整个屏幕高度。

enter image description here

在此处输入图片说明

So the font size was 50with a height of 1024.

所以字体大小为50,高度为1024

Then, I switched to the iPhone SE

然后,我切换到 iPhone SE

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

This was pretty atrocious, so I fixed the font size:

这太糟糕了,所以我修复了字体大小:

enter image description here

在此处输入图片说明

After recording that the font size worked at 25with a height of 568.

在记录字体大小为25和高度为568 之后

Step 2:

第2步:

Bust out the calculator app and find a constantthat relates the font sizesto the heights(in my case), giving or taking a bit.

淘汰计算器应用程序并找到一个字体大小高度相关联的常量(在我的情况下),给予或接受一点。

Here's what I mean:

这就是我的意思:

I knew 50 works with 1024 and that 25 works with 568.

我知道有 50 个与 1024 一起工作,而 25 个与 568 一起工作。

Dividing 50/1024 gives you .048828125.

除以 50/1024 得到 0.048828125。

Multiplying that by 568 equals 27.734375, which would be a little fat of a font size for the SE.

将其乘以 568 等于 27.734375,这对于 SE 的字体大小来说有点胖。

Repeating that process but using the SE's values to do the division produced .0440140845, and checking that with the iPad's height produces a font size of 45, just a bit small for the mighty iPad screen.

重复该过程但使用 SE 的值进行除法产生 0.0440140845,并检查 iPad 的高度产生的字体大小为 45,对于强大的 iPad 屏幕来说只是有点小。

So I split the quotients down the middle, resulting in a number around 0.46.

因此,我将商数从中间拆分,得出的数字约为 0.46。

Step 3:

第 3 步:

Connect the labelsand changetheir font sizes programmaticallyusing this number.

使用此数字以编程方式连接标签更改字体大小

First, drag the labels into an outlet collection. I named my connection 'headerLabels' since they're all at the top of the screen.

首先,将标签拖到插座集合中。我将我的连接命名为“headerLabels”,因为它们都位于屏幕顶部。

enter image description here

在此处输入图片说明

Then, you can kinda steal my code, but just don't make too much money with it ;)

然后,你可以偷我的代码,但不要用它赚太多钱;)

// Outlet collection of labels
@IBOutlet var headerLabels: [UILabel]!

// Who needs a short variable name when we can have a 
// painfully lengthy one to describe the number we calculated?
let relativeFontConstant:CGFloat = 0.046

override func viewDidLoad() {
    super.viewDidLoad()

    // Should be in the viewDidLoad so it can load the same time as
    // the view does.

    // apply a font size to all the labels.
    for label in headerLabels {
        // multiply the height we based it on of the entire view 
        // to the number we calculated
        label.font = label.font.withSize(self.view.frame.height * relativeFontConstant)
    }
}

If you've got any questions, comments, compliments, or insults let me know :)

如果您有任何问题、评论、赞美或侮辱,请告诉我 :)

回答by Kamran Bashir

1. Select your label and open attribute inspector for itSelect the UI (button, label etc)

1. 选择您的标签并为其打开属性检查器选择 UI(按钮、标签等)

2. Click on + sign by Font, select width and height as "Regular", click Add Variationclick on + icon on side of font, select width and height regular

2.点击字体的+号,选择宽度和高度为“常规”,点击添加变化单击字体侧面的 + 图标,选择宽度和高度规则

3. Another Font field will appear, this will represent font for ipad/big screen/ illusion of big screen (scroll view)

3.会出现另一个字体字段,这将代表ipad/大屏幕/大屏幕幻觉的字体(滚动视图)

another font field will appear, this is for big screen (ipad or illusion of big screen by scroll view etc)

将出现另一个字体字段,这是用于大屏幕(ipad 或滚动视图等大屏幕的幻觉)

4. Select your desired font for ipad

4. 选择你想要的 ipad 字体

select the desired size and font you want to show on ipad

选择要在 ipad 上显示的所需大小和字体

回答by Alex Maimescu

I was having the same issue where I needed the text to be scaled proportionally along with the screen size increase.

我遇到了同样的问题,我需要随着屏幕尺寸的增加按比例缩放文本。

Adaptive sizing is quite limited as you can only set the font sizing for size classes. Having the font sizes for two width options, compact and regular was not a solution for me.

自适应大小非常有限,因为您只能为大小类设置字体大小。具有两种宽度选项的字体大小,紧凑和常规对我来说不是解决方案。

I have written a small lib which handles automatic font scaling for UILabeland UITextViewfor different screen sizes.

我写了一个小库,它处理不同屏幕尺寸的UILabelUITextView 的自动字体缩放。

You can set the scaling globally or for a specific instance of UILabeland UITextView.

您可以全局设置缩放比例,也可以为UILabelUITextView的特定实例设置缩放比例。

Find it here: AMXFontAutoScale

在这里找到:AMXFontAutoScale

回答by Dave Hubbard

I had decent behavior with the following code in my viewDidLoad function:

我在 viewDidLoad 函数中使用以下代码有不错的行为:

(Note that the screen bounds are in 'points' not 'pixels')

(请注意,屏幕边界以“点”而不是“像素”为单位)

    // Set the clock font size according to the height.
    //  Design was done on iPhone XR with height of 896 points and font size of 98.
    if (UIScreen.main.bounds.height != 896). // Only need code if not on original design size.
    {
        let scaleFactor: Float = Float(UIScreen.main.bounds.height) / 896.0
        let fontSize = CGFloat(98.0 * scaleFactor)
        self.clock.font = self.clock.font.withSize(fontSize)
    }

This was using the "Helvetica Neue" font which is a fixed width font. It wasn't completely scaled up completely for some reason on larger devices, so still a bit smaller than the target on iPads, but close enough.

这是使用“Helvetica Neue”字体,它是一种固定宽度的字体。由于某种原因,它在较大的设备上并没有完全放大,所以仍然比 iPad 上的目标小一点,但已经足够接近了。

回答by M Naveed Ashfaq

different screen sizes acoording to

根据不同的屏幕尺寸

// iphone 5s,SE screen width 320
// iphone 6,6s,7,etc width 375
// iphone 7 plus, 8 plus, xs max,etc width 414
if (self.view.frame.width == 320) {

    label.font = UIFont(name: label.font.fontName, size: 16)

} else if (self.view.frame.width == 375) {

    label.font = UIFont(name: label.font.fontName, size: 21)

} else if (self.view.frame.width == 414) {

    label.font = UIFont(name: label.font.fontName, size: 24)

}

回答by GoGreen

There is an aspect ratio constraint available. Add this to your label. Constraints to left and top margins for anchoring the label in place should silence the compiler warnings.

有可用的纵横比约束。将此添加到您的标签中。用于将标签锚定到位的左边距和上边距的约束应该使编译器警告静音。

As @VatsalManot mentioned, learn adaptive sizing for starters. Here's a good link:

正如@VatsalManot 提到的,学习初学者的自适应大小。这是一个很好的链接:

http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial

http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial

Hope this helps! :)

希望这可以帮助!:)