【问题标题】:Insert line break for UILabel text when a specific character is found找到特定字符时为 UILabel 文本插入换行符
【发布时间】:2018-03-27 18:26:23
【问题描述】:

我有一个 UILabel,每次单击按钮时都会动态更新,数据将从 firebase 获取并显示在 uilabel 上。我可以显示如下所示的文本。我想在文本中遇到特定分隔符(比如'.' PERIOD)时插入换行符。我研究了许多关于 UIlabel 换行符的解决方案,但找不到我正在寻找的解决方案,每个解决方案都处理我们在情节提要本身中提供的静态文本。任何帮助将不胜感激。谢谢你。 )

【问题讨论】:

    标签: ios swift string text uilabel


    【解决方案1】:

    为标签创建了一个出口,并采用了一个字符串,该字符串包含三个用“.”分隔的句子。附加图像中的内容将是此字符串。尝试使用如下所示的 replaceOccurences() 函数。

    @IBOutlet weak var trialLabel: UILabel!
    
    var string = "This is a trial text.Let us jump to a new line when the full stop is encountered.Hope it helps."
    
    string = string.replacingOccurrences(of: ".", with: ".\n")
    trialLabel.text = string
    

    检查https://developer.apple.com/documentation/foundation/nsstring/1412937-replacingoccurrences 以获取更多参考。快乐编码!

    【讨论】:

    • 我的荣幸...快乐的编码:)
    【解决方案2】:

    您可以使用 UILabel 的属性文本属性来实现这一点。尝试查找并用 html 换行符替换该字符,然后将此文本分配给 UILabel 属性文本。

    你可以用

    替换字符串

    let str = "这是要被新字符串替换的字符串" 让replacedStr = str.replacingOccurrences(of: "string", with: "str")

    【讨论】:

      【解决方案3】:

      使用下面的代码 // HTML标签移除方法

      extension String{
      
          func removeHtmlFromString(inPutString: String) -> String{
      
              return inPutString.replacingOccurrences(of: ".", with: ".\n", options: .regularExpression, range: nil)
          }
      }
      
      
         let str = "Lorem Ipsum is simply dummy text.Lorem Ipsum has been the industry's standard dummy text ever since the 1500."
      
        let postDiscription = str!.removeHtmlFromString(inPutString: str!)
      

      【讨论】:

        【解决方案4】:

        您可以在文本字符串中添加一个 \n 到要创建换行符的位置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-05
          • 2017-02-19
          • 2014-03-11
          • 2019-09-14
          • 1970-01-01
          相关资源
          最近更新 更多