【问题标题】: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
【解决方案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 到要创建换行符的位置。