加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:30:23 所属栏目:百科 来源:网络整理
导读:我正在开发一个iOS应用程序,我在iPhone 5,6,6中观察到UI所有字体都是不同的设备,但我的要求是iPhone4s的按钮大小和字体大小必须相同,5和iPhone 6和6的不同如何在我的应用程序中实现这一目标.我知道我们可以通过编程方式完成,但是有没有机会在故事板上使用自
我正在开发一个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...

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读