我正在完成作业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)
}
所以,我所做的是在初始化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
}
}