微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 如何为iPhone6和6设备设置iphone4s,5和不同的字体和按钮大小?

我正在开发一个iOS应用程序,我在iPhone 5,6,6中观察到UI所有字体都是不同的设备,但我的要求是iPhone4s的按钮大小和字体大小必须相同,5和iPhone 6和6的不同如何在我的应用程序中实现这一目标.我知道我们可以通过编程方式完成,但是有没有机会在故事板上使用自适应布局.

我使用的是xcode7.2,swift2.

提前致谢..

解决方法

我遇到了同样的问题并使用 Swifts Computed Properties解决了它.
我创建了一个静态变量fontsize,由于屏幕的大小,它可以使用适当的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
            }
        }
    }

}

然后使用它来设置按钮标签的fontsize:

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...

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐