Search code examples
swiftxcodeuitextviewmac-catalyst

Textview in tableview is sets text to all caps [Mac Catalyst]


I'm adapting my iPad app to Mac with Mac Catalyst and I am getting an unusual behavior with a UITextView inside a UITableViewCell. When I present the view, I automatically have the textview in focus, but on Mac whatever I type in it, the text shows up capitalized. This have never happened on the iPhone or iPad version. I'm not sure why this is happening (my caps lock is not on), I've tried multiple keyboards and get the same result. It also doesn't happen every time, it's very random.

Here's my code:

class TextViewCell: UITableViewCell {

     override func awakeFromNib() {
          super.awakeFromNib()

          textView.delegate = self
          textView.isScrollEnabled = false
          textView.returnKeyType = .done
     }

     //populate funcion called by the tableview. Just sets the textView into focus if it's the first time the view shows
     func populate(isFirstLoad: Bool = false) {

          //I set isFirstLoad to false after calling on this function in the tableview so this is only called on once
          if isFirstLoad {
               self.titleTF.becomeFirstResponder()
          }
     }
}


// MARK: - textView functions
extension TextViewCell: UITextViewDelegate {
     func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
          if (text == "\n") {
               textView.resignFirstResponder()
               return false
          }
          return true
     }
}

Has anyone else run into this issue and knows how to fix it???


Solution

  • I had the same issue in the past. Since it's on the Mac I do not think capitalization is necessary, so just set textView.autocapitalizationType = .none then it will not have that strange bug. You can also wrap it around in a #if targetEnvironment(macCatalyst) so it will only do this on Mac