【问题标题】:Stanford CS193p - Changing font size in line with card count number?斯坦福 CS193p - 根据卡数更改字体大小?
【发布时间】:2020-09-13 15:31:39
【问题描述】:

我正在处理作业 1 的最后一项任务,“5。当您的游戏随机显示 5 对时,我们用于表情符号的字体将太大(纵向)并且会开始被剪裁。有在 5 对情况下(仅)调整字体以使用比 .largeTitle 更小的字体。当游戏中有 4 对或更少对时继续使用 .largeTitle。”

我在下面添加了以下代码,但无法弄清楚如何正确执行。非常感谢任何帮助。为那些超级足以回答的人编码的新手!

struct ContentView: View {
var viewModel: EmojiMemoryGame

var body: some View {
    HStack {
        ForEach(viewModel.cards) { card in
            CardView(card: card).onTapGesture {
                self.viewModel.choose(card: card)
            }
// MARK: - Aspect Ratio 2/3
        }.aspectRatio(2/3, contentMode: .fit)
    }
        .padding()
        .foregroundColor(Color.orange)
  //.font(Font.largeTitle).   To be deleted once the below is made successful

  //TODO: - Add font size to allow 5 pairs of cards not to clip on portrait view

  if numberOfPairsOfCards.count >=5 {
            .font(Font.footnote)
        } else {
            .font(Font.largeTitle)
        }

【问题讨论】:

    标签: ios swift xcode font-size emoji


    【解决方案1】:

    我使用了三元运算符

    struct ContentView: View {
    var viewModel: EmojiMemoryGame
    
    var body: some View {
        HStack {
            ForEach(viewModel.cards) { card in
                CardView(card: card).onTapGesture {
                    self.viewModel.choose(card: card)
                }
                .aspectRatio(2/3, contentMode: .fit)
            }
        }
        .padding()
        .foregroundColor(Color.orange)
        .font(viewModel.cards.count == 10 ? Font.footnote : Font.largeTitle)
    }}
    

    【讨论】:

      【解决方案2】:

      所以我所做的就是在初始化 ContentView 时添加一个参数,因为我发现在 contentView 中很多东西是不可变的。所以我在 SceneDelagate 中有一行来处理字体类型

      let game = EmojiMemoryGame()
          var fontType: Font
          if game.cards.count == 10{
              fontType = Font.body
          } else {
              fontType = Font.largeTitle
          }
          let contentView = ContentView(game_viewModel: game, fontType: fontType)
      

      这是我的 ContentView 代码

      struct ContentView: View {
      var game_viewModel: EmojiMemoryGame
      var fontType: Font
      var body: some View {
          let cardStack = HStack {
              ForEach(game_viewModel.cards) { card in
                  CardView(card: card).onTapGesture{
                      self.game_viewModel.choose(card: card)
                  }
              }
          }
              .padding()
              .foregroundColor(Color.orange)
              .font(fontType)
          return cardStack
      }
      }
      

      另一种方法是添加一个更新字体的函数:

      struct ContentView: View {
      var game_viewModel: EmojiMemoryGame
      func updatefont() -> Font{
          if game_viewModel.cards.count == 10{
              return Font.body
          } else {
              return Font.largeTitle
          }
      }
      var body: some View {
          let cardStack = HStack {
              ForEach(game_viewModel.cards) { card in
                  CardView(card: card).onTapGesture{
                      self.game_viewModel.choose(card: card)
                  }
              }
          }
              .padding()
              .foregroundColor(Color.orange)
              .font(updatefont())
          return cardStack
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 2018-02-22
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多