如何以编程方式设置wC hR字体大小?

问题描述:

enter image description here如何以编程方式设置wC hR字体大小?

我使用约束为iPad和iPhone .. 所以现在我想改变按钮字体大小(WC HR)prograamatically。

+0

http://*.com/questions/1465305/set-uibutton-title-uilabel-font-size-programmatically – Skywalker

let wSize = self.view.traitCollection.horizontalSizeClass 
let hSize = self.view.traitCollection.verticalSizeClass 

if wSize == UIUserInterfaceSizeClass.Compact && hSize == UIUserInterfaceSizeClass.Regular { 
    button.titleLabel?.font = UIFont(name: FontNameCode, size: size) 
} 
+0

我正在使用Objective C.你能给我提供代码吗? –

+0

对不起,我在你的文章中遗漏了这个标签。我真的不知道Obj-C足够好转换它。但是你只需要创建两个'UIUserInterfaceSizeClass'对象,然后检查它们是否合适。 – Tim

设置字体大小如下。

步骤1:集字体作为系统尺寸等级

enter image description here

步骤2:子类的UILabel和覆盖 “layoutSubviews” 方法等:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    // Implement font logic depending on screen size 
    self.font = [UIFont systemFontOfSize:self.font.pointSize]; 
} 

可能它会帮助你。