Post

Replies

Boosts

Views

Activity

Reply to Text like PlaceHolder
Well, I think I found a much simpler solution: create a background image with the text for placeholder then, when you type, the new text will appear just over the placeholder. I've left some links to additional information if needed Here is a sample code: class TextFieldDynPlaceHolder: UITextField, UITextFieldDelegate { let defaultPlaceHolder = "DD MM YYYY" var thePlaceHolder : String! // To be set at call func commonInit() { delegate = self thePlaceHolder = defaultPlaceHolder // "DD MM YYYY" self.background = image(from: thePlaceHolder) // Need to set borderStyle: https://stackoverflow.com/questions/64057501/how-to-fix-uitextfield-background-image-not-displayed-after-ios14 self.borderStyle = .line } // https://stackoverflow.com/questions/759658/uitextview-background-image override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } // https://stackoverflow.com/questions/51100121/how-to-generate-an-uiimage-from-custom-text-in-swift func image(from text: String?) -> UIImage? { let frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) let nameLabel = UILabel(frame: frame) nameLabel.textAlignment = .left nameLabel.backgroundColor = .systemBackground nameLabel.textColor = .placeholderText // .darkGray if .placeholderText is too light nameLabel.font = self.font nameLabel.text = text UIGraphicsBeginImageContext(frame.size) if let currentContext = UIGraphicsGetCurrentContext() { nameLabel.layer.render(in: currentContext) let nameImage = UIGraphicsGetImageFromCurrentImageContext() return nameImage } return nil } // Check we typed digits or space or backspace func textFieldDidChangeSelection(_ textField: UITextField) { var trimmedPlaceHolder = defaultPlaceHolder if let typedText = textField.text { for c in typedText.unicodeScalars { if !CharacterSet.decimalDigits.contains(c) && !CharacterSet.whitespaces.contains(c) { // And play a beep textField.text = String(textField.text!.dropLast()) } } } if let typed = textField.text?.count, typed > 0 { trimmedPlaceHolder = String(defaultPlaceHolder.dropFirst(typed)) // And we replace with spaces for _ in 0..<typed { trimmedPlaceHolder = " " + trimmedPlaceHolder } self.background = image(from: trimmedPlaceHolder) } else { self.background = image(from: trimmedPlaceHolder) } } // Could check here the complete date Validity func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return true // Here you could test the validity of entry as a Date } } To test it: in storyboard, create a UITextField and declare as TextFieldDynPlaceHolder select a fixed width font (otherwise, date will nor superpose placeholder) set its width to what you need (don't let it free, otherwise placeholder may not show) in the controller, set thePlaceHolder to the date you need May set thePlaceHolder in commonInit() (change from = "DD MM YYYY") If that works, thanks to tell as well as to post any improvement you may find, like better testing the validity of what was typed. And then don't forget to close the thread.
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Trailing closure passed to parameter of type 'Int' that does not accept a closure
Which line exactly ? Is it line 3 or 4 ? If so, please show how onboardingData and OnboardingCard are defined. Otherwise tell exactly where you get the error. 1. VStack { 2. TabView { 3. ForEach(onboardingData) { onboardingItem in 4. OnboardingCard(onboardingItem: onboardingItem) 5. } 6. } 7. .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic)) 8. .indexViewStyle(PageIndexViewStyle (backgroundDisplayMode: .always)) 9. .foregroundColor(.white) 10. }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Text like PlaceHolder
In previous code, there are some issues with darkmode: placeholder hardly readable (a known bug in iOS in dark mode) when switching mode, colors did not immediately adjust. Here is the improved code: class TextFieldDynPlaceHolder: UITextField, UITextFieldDelegate { let defaultPlaceHolder = "DD MM YYYY" // Could set it as IBInspectable property var thePlaceHolder : String! // To be set at call // --------------------- commonInit ---------------------------------------------------- // Description: set the placeholder from text // Parameters // Comments: // Need to set borderStyle (bug in iOS): https://stackoverflow.com/questions/64057501/how-to-fix-uitextfield-background-image-not-displayed-after-ios14 // ------------------------------------------------------------------------------------------------- func commonInit() { delegate = self thePlaceHolder = defaultPlaceHolder // "DD MM YYYY" self.background = image(from: thePlaceHolder) self.borderStyle = .line } override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } // --------------------- traitCollectionDidChange ------------------------------------------- // Description: Adjust colors when switching mode light / dark. // Parameters // previousTraitCollection: UITraitCollection? // Comments: // ------------------------------------------------------------------------------------------------- override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) adaptPlaceHolder(self) } // --------------------- image ------------------------------------------- // Description: Create image from a text. // Parameters // from text: String? // Comments: // https://stackoverflow.com/questions/51100121/how-to-generate-an-uiimage-from-custom-text-in-swift // In fact, placeholderText unreadable in darkMode. https://stackoverflow.com/questions/58478744/uitextfield-placeholder-text-is-unreadable-in-ios13-dark-mode // ------------------------------------------------------------------------------------------------- func image(from text: String?) -> UIImage? { let frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) let nameLabel = UILabel(frame: frame) nameLabel.textAlignment = .left nameLabel.backgroundColor = .systemBackground if traitCollection.userInterfaceStyle == .light { nameLabel.textColor = .placeholderText // .darkGray if .placeholderText is too light } else { nameLabel.textColor = .systemGray } nameLabel.font = self.font nameLabel.text = text UIGraphicsBeginImageContext(frame.size) if let currentContext = UIGraphicsGetCurrentContext() { nameLabel.layer.render(in: currentContext) let nameImage = UIGraphicsGetImageFromCurrentImageContext() return nameImage } return nil } // --------------------- adaptPlaceHolder -------------------------------------- // Description: Trims placeHolder text when first characters typed ; rebuilds image. // Parameters // textField: UITextField // Comments: // ------------------------------------------------------------------------------------------------- func adaptPlaceHolder(_ textField: UITextField) { var trimmedPlaceHolder = defaultPlaceHolder if let typed = textField.text?.count, typed > 0 { trimmedPlaceHolder = String(defaultPlaceHolder.dropFirst(typed)) // And we replace with spaces for _ in 0..<typed { trimmedPlaceHolder = " " + trimmedPlaceHolder } self.background = image(from: trimmedPlaceHolder) } else { self.background = image(from: trimmedPlaceHolder) } } // --------------------- adaptPlaceHolder -------------------------------------- // Description: redraws placeHolder image when characters typed and checks characters are decimals // Parameters // textField: UITextField // Comments: // ------------------------------------------------------------------------------------------------- func textFieldDidChangeSelection(_ textField: UITextField) { if let typedText = textField.text { for c in typedText.unicodeScalars { if !CharacterSet.decimalDigits.contains(c) && !CharacterSet.whitespaces.contains(c) { // And play a beep textField.text = String(textField.text!.dropLast()) } } } adaptPlaceHolder(textField) } // --------------------- textField -------------------------------------- // Description: test the validity of entry (as a Date for instance) // Parameters // textField: UITextField // shouldChangeCharactersIn range: NSRange // replacementString string: String // Comments: // To be implemented // ------------------------------------------------------------------------------------------------- func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return true } }
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Trailing closure passed to parameter of type 'Int' that does not accept a closure
Doc states : Tab views only support tab items of type Text, Image, or an image followed by text. Passing any other type of view results in a visible but empty tab item. like in TabView { Text("The First Tab") .badge(10) .tabItem { Image(systemName: "1.square.fill") Text("First") } That's not the case with your code: you return OnboardingCard ForEach(onboardingData) { onboardingItem in OnboardingCard(onboardingItem: onboardingItem) } What is the purpose of VStack for TabView ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to programmatic constraints and Interface Builder
I did not dig into details, just found some interesting thread; problem may be how you use translatesAutoresizingMaskIntoConstraints: https://stackoverflow.com/questions/45466466/ibdesignable-view-with-dynamic-constraints-misplaced-children-when-rendering and maybe this one: https://stackoverflow.com/questions/43002115/update-constraints-for-ibdesignable Do you have a M1 Mac ? If so, look at this: https://developer.apple.com/forums/thread/665826 PS: Why don't you do it the other way: design in IB and adapt some constraints in code with ntheir IBObject where needed ?
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Started to learn Swift and got stuck...
A possible reason is that your selection in the source file is not correct. You need to select precisely what I marked in green. If you select also the next line (with }) or forget the last line in green, you get the problem. Note: it is usually better to explicit the type of sender : @IBAction func buttonPressed(_ sender: UIButton) {
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Xcode: Keyboard autorepeat
I never noticed, but you're right. And could not find anyway to change this behaviour in preferencesces. However, that looks like a design decision ; you get the alternate key and not the autorepeat. Maybe the reason is to avoid accidental typing errors in code ? Note : that's the same when you type text in this forum. Feel free to send a bug report for improvement suggestion.
Aug ’21
Reply to Xcode13 beta -> Xcode 12 issue
it does not depend on the iOS version Are you so sure ? What you show are swith iOS 15 and iOS 14. Could you test also with Xcode 13ß but simulator iOS14 ? Those are not blackboxes, they are the view background which is black. And same issue with the other light gray at top. Could you please show the complete code for the viewController as well as the screenshots of Attributes inspector for the view of the ViewController ? As this one:
Aug ’21