【问题标题】:How to set the font and button sizes same for iphone4s, 5 and different for iPhone6 and 6+ devices?如何为 iphone 4s、5 设置相同的字体和按钮大小,而为 iPhone 6 和 6+ 设备设置不同的字体和按钮大小?
【发布时间】:2016-04-15 18:56:38
【问题描述】:

我正在开发一个 iOS 应用程序,我观察到 iPhone 5、6、6+ 中的 UI 所有字体因设备而异,但我的要求是 iPhone4s 和 5 的按钮大小和字体大小必须相同,而iPhone 6 和 6+。如何在我的应用程序中实现这一点。我知道我们可以通过编程来完成,但是有没有机会在情节提要上使用自适应布局。

我正在使用 xcode7.2,swift2。

提前谢谢..

【问题讨论】:

标签: swift storyboard adaptive-layout


【解决方案1】:

我遇到了同样的问题,并使用 Swifts Computed Properties 解决了它。 我创建了一个静态变量 fontsize,由于屏幕大小,它会使用适当的字体大小动态初始化。

import UIKit

class ViewFunctions {

    let screenSize = UIScreen.mainScreen().bounds.size
    static var fontsize: CGFloat {
        get {
            if screenSize.height >= 1024 { // iPad Pro
                return 16.0
            } else if screenSize.height >= 768 { // iPad
                return 16.0
            } else if screenSize.height >= 414 { // iPhone 6Plus
                return 15.0
            } else if screenSize.height >= 375 { // iPhone 6/s
                return 15.0
            } else if screenSize.height >= 320 { // iPhone 5/s
                return 14.0
            } else if screenSize.height >= 319 { // iPhone 4/s
                return 14.0
            } else {
                return 14.0
            }
        }
    }

}

然后使用它来设置按钮标签的字体大小:

import UIKit

class TestClass {    

    var testButton: UIButton = UIButton(type: UIButtonType.System) as UIButton!
    testButton.titleLabel!.font = UIFont(name: "Helvetica Neue", size: ViewFunctions.fontsize)
    testButton.setTitle("Test", forState: .Normal)
    // add button to view or sth like that...

}

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2016-01-14
    • 2015-08-16
    • 2014-03-25
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多