iOS UILabel中特殊字符之后的新行



我需要知道如何在UILabel中自动输入新行(就像用于换行符一样(,但基于特殊字符。

例如,我在变量 =>
let a = "这是动物的一个例子:- 猫 - 狗 - 鸟"。

通常,我的 UILabel 文本将显示如下 =>"这是动物的一个例子:- 猫 - 狗 - 鸟"。

我想要的是UILabel显示这样的文本"这是动物的一个例子:
-猫
-狗
- 鸟">

因此,在"-"字符之后,它会自动创建一个新行,就像字符串中的""一样。

提前致谢

label.text = "This is an example of animal :n - Catn - Dogn - Bird"
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping

numberOfLines设置为 0。 然后可以使用多行

或者,如果您希望代码为您执行此操作:

let aString = "This is an example of animal : - Cat  - Dog - Bird"
let newString = aString.replacingOccurrences(of: "-", with: "n-", options: .literal, range: nil)
label.text = newString

您可以将"-"替换为"",如下面的代码。它在 swift4 中工作。

var tmpStr = "This is an example of animal : - Cat - Dog - Bird".replacingOccurrences(of: "-", with: "n-")
yourLabel.text = tmpStr
yourLabel.numberOfLines = 0
yourLabel.lineBreakMode = .byWordWrapping

最新更新