【问题标题】:Having trouble scaling the font-size to the frame's height/width无法将字体大小缩放到框架的高度/宽度
【发布时间】:2020-06-09 18:38:24
【问题描述】:

我使用 iPhone 11 作为基准,我试图在 iPad 上运行它时放大字体大小,但我无法让它工作。

我以编程方式完成所有工作,并且还在视图、文本字段、标签和文本视图上使用自动布局。我尝试使用 adjustsFontSizeToWidth 和 minimumScaleFactor, numberOfLines 但无济于事。当我扩展到 iPad 时,我无法更改字体大小。这是它的样子......

我还在学习,我对编程和 Swift 还比较陌生,我之前尝试过问这个问题,但我想我的问题太含糊了。

如果您需要我在其中包含一些代码,请告诉我。

【问题讨论】:

    标签: swift xcode font-size


    【解决方案1】:

    我也以编程方式做所有事情,我建议您使用 UIKitPlus 库,它将解决您的很多问题

    例如您可以轻松地为不同的设备或设备类型设置不同的字体大小

    TextView().font(.helveticaNeueRegular, 16 !! .iPad(24))
    

    正如您在上面的示例中看到的,您可以像往常一样设置一些值,也可以通过!! 运算符为不同类型的设备槽提供另一个值。

    使用起来非常简单方便。

    整个视图控制器可能看起来像

    import UIKitPlus
    
    class MyViewController: ViewController {
        @State var weight = ""
        @State var repetitions = ""
        @State var notes = ""
    
        override func buildUI() {
            super.buildUI()
            body {
                VStack {
                    Text("Bench Press")
                        .color(.black)
                        .font(.articoBold, 32)
                        .edgesToSuperview(top: 16, leading: 16)
                    VSpace(32)
                    TextField($weight)
                        .placeholder(AttrStr("Total weight").foreground(.lightGray).font(.articoRegular, 14))
                        .leftView(HStack {
                            HSpace(4)
                            Text("Weight (lbs)").color(.black).font(.articoMedium, 14)
                            HSpace(4)
                        })
                        .color(.black)
                        .font(.articoRegular, 14)
                        .keyboard(.numbersAndPunctuation)
                        .returnKeyType(.next)
                        .tag(0)
                        .shouldReturnToNextResponder()
                    TextField($repetitions)
                        .placeholder(AttrStr("Number or Reps...").foreground(.lightGray).font(.articoRegular, 14))
                        .leftView(HStack {
                            HSpace(4)
                            Text("Repetitions").color(.black).font(.articoMedium, 14)
                            HSpace(4)
                        })
                        .color(.black)
                        .font(.articoRegular, 14)
                        .keyboard(.numbersAndPunctuation)
                        .returnKeyType(.next)
                        .tag(1)
                        .shouldReturn { $0.resignFirstResponder() }
                    VSpace(32)
                    HStack {
                        Image(nil).size(40)
                        HSpace(8)
                        Text("Timer").color(.lightGray).font(.articoMedium, 14)
                    }
                    .alignment(.center)
                    VSpace(32)
                    TextView($notes)
                        .placeholder("Notes...")
                        .color(.black)
                        .font(.articoRegular, 14)
                    VSpace(32)
                    Text("Set 1 0 lbs - 0 Reps").color(.black).font(.articoRegular, 14)
                    VSpace(16)
                    Text("Set 2 0 lbs - 0 Reps").color(.black).font(.articoRegular, 14)
                    VSpace(16)
                    Text("Set 3 0 lbs - 0 Reps").color(.black).font(.articoRegular, 14)
                    VSpace(16)
                    Text("Set 4 0 lbs - 0 Reps").color(.black).font(.articoRegular, 14)
                    Space()
                    HStack {
                        Button("Next Set").onTapGesture {
                            print("next set tapped")
                        }
                        Button("Next Excercise").onTapGesture {
                            print("next excercise tapped")
                        }
                    }
                    VSpace(32)
                }
                .alignment(.center)
                .edgesToSuperview()
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2021-02-13
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多