ios 如何在 Swift 中更改 UILabel 的字体大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24356888/
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 01:03:06  来源:igfitidea点击:

How do I change the font size of a UILabel in Swift?

iosuilabelswift

提问by Jay

label.font.pointSizeis read-only, so I'm not sure how to change it.

label.font.pointSize是只读的,所以我不确定如何更改它。

回答by Connor

You can do it like this:

你可以这样做:

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

Or like this:

或者像这样:

label.font = label.font.withSize(20)

This will use the same font. 20 can be whatever size you want of course.

这将使用相同的字体。20当然可以是你想要的任何尺寸。

Note:The latter option will overwrite the current font weight to regularso if you want to preserve the font weight use the first option.

注意:后一个选项将覆盖当前字体粗细,regular因此如果您想保留字体粗细,请使用第一个选项。

Swift 3 Update:

斯威夫特 3 更新

label.font = label.font.withSize(20)

Swift 4 Update:

斯威夫特 4 更新

label.font = label.font.withSize(20)

or

或者

label.font = UIFont(name:"fontname", size: 20.0)

and if you use the system fonts

如果你使用系统字体

label.font = UIFont.systemFont(ofSize: 20.0)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.font = UIFont.italicSystemFont(ofSize: 20.0)

回答by mouselangelo

I think the best way to do this - if keeping the same font that is already assigned to the UILabelwould be:

我认为最好的方法是 - 如果保持已经分配给的相同字体UILabel将是:

(using Swift)

(使用斯威夫特)

label.font = label.font.fontWithSize(20)

(using Swift 3)

(使用 Swift 3)

label.font = label.font.withSize(20)

Ideally I would set this in the viewDidLayoutSubviewsmethod, as it doesn't need to change every time the view appears.

理想情况下,我会在viewDidLayoutSubviews方法中设置它,因为它不需要在每次出现视图时更改。

回答by ma11hew28

label.font = UIFont.systemFontOfSize(20)

回答by Gautam Sareriya

We can set font as per our requirement like,

我们可以根据我们的要求设置字体,例如,

label.font = UIFont(name: "Avenir-Light", size: 15.0)
label.font = UIFont.boldSystemFontOfSize(15)
label.font = UIFont.italicSystemFontOfSize(15)
label.font = UIFont.systemFontOfSize(17)

回答by YannSteph

If you want just change size of your font i create this extension

如果您只想更改字体大小,我会创建此扩展程序

// Add extension

extension UILabel {
    func setSizeFont (sizeFont: Double) {
        self.font =  UIFont(name: self.font.fontName, size: sizeFont)!
        self.sizeToFit()
    }
}

// Use

myLabel.setSizeFont(60)

回答by Santo

You can give like this also

你也可以这样给

labelName.font = UIFont(name: "systemFont", size: 30)

回答by David DelMonte

In Swift 3 again...

再次在 Swift 3 中...

myLabel.font = myLabel.font.withSize(18)

回答by aqsa arshad

Swift-3.1

Swift-3.1

label.font = UIFont.systemFont(ofSize: 12)

label.font = UIFont.systemFont(ofSize: 12)

回答by Dilip Jangid

In swift3, suppose your UILable name is myLable and you want to change its font size do this

在 swift3 中,假设您的 UILable 名称是 myLable 并且您想更改其字体大小,请执行以下操作

myLable.font = UIFont.systemFont(ofSize: 10)

回答by lhmgrassi

You can use an extension.

您可以使用扩展程序。

import UIKit

extension UILabel {

    func sizeFont(_ size: CGFloat) {
        self.font = self.font.withSize(size)
    }
}

To use it:

要使用它:

self.myLabel.fontSize(100)