简体   繁体   English

如何使用Swift默认UILabel字体和大小

[英]How to default UILabel Font and Size using Swift

I find UISegmentedControl change font and size like this : 我发现UISegmentedControl改变了这样的字体和大小:

UISegmentedControl.appearance().setTitleTextAttributes(myFontAttribute as [NSObject : AnyObject] , forState: .Normal)

but UILabel have no this method 但是UILabel没有这种方法

I want to do like 我想这样做

UILabel.appearance().setAttributed(myFontAttribute)

I don't want to change UILabel font in StoryBoard 我不想在StoryBoard中更改UILabel字体

I want to using program to do this (because my app is done, but only font should change to bigger and other font) 我想使用程序来执行此操作(因为我的应用程序已完成,但只有字体应更改为更大和其他字体)

What should I do ? 我该怎么办 ?

First you need to add extension to UILabel : 首先,您需要为UILabel添加扩展:

extension UILabel{
    var defaultFont: UIFont? {
        get { return self.font }
        set { self.font = newValue }
    }
}

Second use appearance to set it: 第二次使用外观设置它:

    UILabel.appearance().defaultFont = UIFont.systemFont(ofSize: 25)

Hope it helps. 希望能帮助到你。

You can change the label font programmatically like this 您可以像这样以编程方式更改标签字体

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

Change font size only with bold 仅使用粗体更改字体大小

label.font = UIFont.boldSystemFontOfSize(18)

Change font size only 仅更改字体大小

label.font = label.font.fontWithSize(14)

if you want to change the size of font, used below lines of code : 如果你想改变字体的大小,用在下面的代码行:

for Swift 3 : 对于Swift 3:

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

你可以在swift中使用这个简单的代码

myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()])

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

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