如何在Swift中更改UILabel的字体大小?
问题描述:
label.font.pointSize
是只读的,所以我不确定如何更改它.
label.font.pointSize
is read-only, so I'm not sure how to change it.
答
您可以这样做:
label.font = UIFont(name: label.font.fontName, size: 20)
或者像这样:
label.font = label.font.withSize(20)
这将使用相同的字体.当然,您想要的大小可以是20.
This will use the same font. 20 can be whatever size you want of course.
注意:后一个选项会将当前字体粗细覆盖为 regular
,因此,如果要保留字体粗细,请使用第一个选项.
Note: The latter option will overwrite the current font weight to regular
so if you want to preserve the font weight use the first option.
迅速3更新:
label.font = label.font.withSize(20)
迅速4更新:
label.font = label.font.withSize(20)
或
label.font = UIFont(name:"fontname", size: 20.0)
,如果您使用系统字体
label.font = UIFont.systemFont(ofSize: 20.0)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.font = UIFont.italicSystemFont(ofSize: 20.0)