【问题标题】:How to remove \n\n from UITextView using swift code?如何使用 swift 代码从 UITextView 中删除 \n\n?
【发布时间】:2020-08-21 19:01:37
【问题描述】:

我让用户在 UITextView 中输入文本,然后使用此代码将其发送到 firebase

         func sendDataToFirebase(){
            
              let productID = self.ref.childByAutoId().key
              let userID = Auth.auth().currentUser?.uid
           
              let product = [ "productID" : productID,
                              "uid" : userID,
                              "details": self.txtProductDetails.text! as String
        
                ] as [String : Any]
       
            self.ref.child("products").child(productID!).setValue(product)

           }

如果用户输入的文本是多行的,它将转到 firebase 并具有 \n\n

因此,当我获取文本时,我得到的结果与 \n\n 相同。如何删除 \n\n textView 必须看起来有条理的地方。

【问题讨论】:

标签: swift firebase firebase-realtime-database swift4 uitextview


【解决方案1】:

试试这个

  func sendDataToFirebase(){
        
          let productID = self.ref.childByAutoId().key
          let userID = Auth.auth().currentUser?.uid
          guard let text = self.txtProductDetails.text else{return}
          let newText = text.replacingOccurrences(of: "\n\n", with: "")
       
          let product = [ "productID" : productID,
                          "uid" : userID,
                          "details": newText
    
            ] as [String : Any]
   
        self.ref.child("products").child(productID!).setValue(product)

       }

【讨论】:

  • guard let text = self.txtProductDetails.text else { return } 这是毫无意义的。 UITextView 文本属性永远不会返回 nil
  • 是的,但是当您看到可选的选项时,最好安全地打开它。
  • 好答案。一题。如果将 /n/n 替换为 "",则 /n/n 应该/可能出现的所有空格都会丢失。所以回读它看起来像几个月来制作。大小(“。”和“The”之间没有空格)。可能最好添加一个空格“”。只是一个想法。
  • 是的,你是对的。但是文字图片在\n\n之前有空格(比如大号和中号\n\n的权重)。所以,我认为没有空间它的结构会更好。
猜你喜欢
  • 2019-07-17
  • 2022-01-24
  • 2012-04-09
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多