此代码有效但正如我所说,我不想改变大小
QuotesLabel.font = UIFont(name: "optima", size: CGFloat(30)
这是不同设备的不同尺寸的代码
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
QuotesLabel.font = QuotesLabel.font.fontWithSize(30)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
QuotesLabel.font = QuotesLabel.font.fontWithSize(30)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
QuotesLabel.font = QuotesLabel.font.fontWithSize(35)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
QuotesLabel.font = QuotesLabel.font.fontWithSize(40)
}
答案 0 :(得分:5)
您可以通过以下方式指定自己的字体大小:
QuotesLabel.font = UIFont(name: "optima", size: QuotesLabel.font.pointSize)
<强>更新强>
您可以这样创建和使用字体数组:
let fontArr = ["helvetica", "optima", "arial"] //Change this array as per your need
let int = Int(arc4random_uniform(UInt32(fontArr.count)))
QuotesLabel.font = UIFont(name: fontArr[int], size: QuotesLabel.font.pointSize)
答案 1 :(得分:2)
使用此UIFont扩展程序:
extension UIFont{
func fontWithName(name:String)->UIFont{
return UIFont(name: name, size: self.pointSize)!
}
然后像这样使用它:
QuotesLabel.font = QuotesLabel.font.fontWithName("Verdana"
UPDATE :数组中的随机字体
定义所需的字体数组:
let fonts = ["Verdana", "HoeflerText-Black", "Menlo-BoldItalic"]
此函数将返回随机字体:
func getRandomFont()->UIFont{
let int = Int(arc4random_uniform(UInt32(fonts.count)))
let font = UIFont(name: fonts[int], size: 30)
return font
}
开启按钮点按动作调用随机功能并更改TextField字体:
@IBAction func testTapped(sender: UIButton) {
QuotesLabel.font = getRandomFont()
}