英文:
How to remove 3 dots after end of line limit
问题
在SwiftUI中,
Text("a \n b \n c \n d \n e")
.lineLimit(3)
在SwiftUI中,上面的代码显示的输出包括末尾的3个点。
输出:
a
b
c...
但我的目标是显示没有点的输出,就像这样 -
目标:
a
b
c
英文:
In SwiftUI,
Text("a \n b \n c \n d \n e")
.lineLimit(3)
In SwiftUI, the above code shows output including 3 dots in the end.
Output:
a
b
c...
But my target is to show the output without dots like this -
Target:
a
b
c
答案1
得分: 1
实现以下方式。
Text("1\n 2 \n 3 \n 4 \n 5".truncateToLineLimit(3))
extension String {
func truncateToLineLimit(_ lineLimit: Int) -> String {
var truncatedString = ""
let lines = self.components(separatedBy: "\n").prefix(lineLimit)
for line in lines {
truncatedString += line.trimmingCharacters(in: .whitespacesAndNewlines)
if line != lines.last {
truncatedString += "\n"
}
}
return truncatedString
}
}
英文:
implement the following way.
Text("1\n 2 \n 3 \n 4 \n 5".truncateToLineLimit(3))
extension String {
func truncateToLineLimit(_ lineLimit: Int) -> String {
var truncatedString = ""
let lines = self.components(separatedBy: "\n").prefix(lineLimit)
for line in lines {
truncatedString += line.trimmingCharacters(in: .whitespacesAndNewlines)
if line != lines.last {
truncatedString += "\n"
}
}
return truncatedString
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论