UILabel - setting font - typeface programmatically in iPhone

后端 未结 6 965
谎友^
谎友^ 2020-12-05 02:40

I have following code in my Application.

tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
[tmp setFont:[UIFont         


        
6条回答
  •  感情败类
    2020-12-05 03:23

    Swift update

    If you've already added the font to the app (as explained here) you can use Swift's extension feature with, as example, a UILabel and the Roboto font:

    extension UILabel {
    
        func setFont(style: String, size: Int){
            self.font = UIFont(name: style, size: CGFloat(size))
        }
    
        struct FontStyle {
            public static let italic: String = "Roboto-LightItalic"
            public static let normal: String = "Roboto-Light"
            public static let thin: String = "Roboto-Thin"
        }
    }
    

    Roboto-LightItalic and Roboto-Light are the TTF's font file names. Then, you can simply call

    someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20)
    

提交回复
热议问题