leontedev / WordScrambleSwiftUI

SwiftUI - Project 5 - HackingWithSwift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WordScramble

A simple SwiftUI word game.

gif.gif

Day 29

Day 30

  • View Modifier .onAppear(perform:) takes a function name as a parameter, to be executed before the UI is drawn.
}.navigationBarTitle(rootWord)
 .onAppear(perform: startGame)
  • TextField’s onCommit parameter takes a function name to be executed when the user presses return on the keyboard
  • TextField’s .autocapitalization(.none) view modifier to disable the default behavior
  • Using SFSymbols easily using the Image(systemName:)
// the below is the same as List { ForEach(usedWords, id:\.self) {
List(usedWords, id:\.self) { // id: is used to uniquely identify each row  
    Image(systemName: "\($0.count).circle") // 1.circle is a SFSymbol
    Text($0) // List automatically places its contents into an HStack - so it can be omitted
}

Day 31 - Challenge

  1. Disallow answers that are shorter than three letters or are just our start word. For the three-letter check, the easiest thing to do is put a check into isReal() that returns false if the word length is under three letters. For the second part, just compare the start word against their input word and return false if they are the same.
  2. Add a left bar button item that calls startGame(), so users can restart with a new word whenever they want to.
  3. Put a text view below the List so you can track and show the player’s score for a given root word. How you calculate score is down to you, but something involving number of words and their letter count would be reasonable.

Day 31 - Personal Touch

  • Show All Permutations
  • Using the Dictionary instead of UITextChecker. I have noticed that there are strings which are accepted by UITextChecker as being spelled correctly even though they are not words in the dictionary (eg: ffds, fda etc). Probably because they are abbreviations of different sorts.
func isReal(word: String) -> Bool {
        if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) {
            return true
        } else {
            return false
        }
}

UIReferenceLibraryViewController is part of UIKit - so that needs to be imported as well. It’s also able to instantiante a UIKit view with the full definition of the word and buttons to manage dictionaries, look online etc:

I’ve tried to integrate it in SwiftUI - which is pretty straight-forward using UIViewControllerRepresentable:

import SwiftUI
import UIKit

struct UIReferenceLibraryController: UIViewControllerRepresentable {
    var word: String
    
    func makeUIViewController(context: Context) -> UIReferenceLibraryViewController {
            let referenceViewController = UIReferenceLibraryViewController(term: word)

            return referenceViewController
    }
    
    func updateUIViewController(_ referenceViewController: UIReferenceLibraryViewController, context: Context) {
        UIReferenceLibraryViewController(term: word)
    }
    
}

Day 75 - Accessibility improvement

Our List view relies on an implicit HStack to place the image and text side by side. So, to fix this we need to create an explicit HStack so we can apply our VoiceOver customization. We will ignore the children of the HStack (the image which shows the number of letters and the word) and instead provide a custom label.

List(usedWords, id: \.self) { word in
    HStack {
        Image(systemName: "\(word.count).circle")
        Text(word)
    }
    .accessibilityElement(children: .ignore)
    .accessibility(label: Text("\(word), \(word.count) letters"))
}

Day 94 - Challenges #2 & #3

  1. Change project 5 (Word Scramble) so that words towards the bottom of the list slide in from the right as you scroll. Ideally at least the top 8-10 words should all be positioned normally, but after that they should be offset increasingly to the right.
  2. For a real challenge make the letter count images in project 5 change color as you scroll. For the best effect, you should create colors using the Color(red:green:blue:) initializer, feeding in values for whichever of red, green, and blue you want to modify. The values to input can be figured out using the row’s current position divided by maximum position, which should give you values in the range 0 to 1.

About

SwiftUI - Project 5 - HackingWithSwift.com


Languages

Language:Swift 100.0%