如何在行限制结束后删除3个点。

huangapple go评论93阅读模式
英文:

How to remove 3 dots after end of line limit

问题

在SwiftUI中,

  1. Text("a \n b \n c \n d \n e")
  2. .lineLimit(3)

在SwiftUI中,上面的代码显示的输出包括末尾的3个点。
输出:

  1. a
  2. b
  3. c...

但我的目标是显示没有点的输出,就像这样 -
目标:

  1. a
  2. b
  3. c
英文:

In SwiftUI,

  1. Text("a \n b \n c \n d \n e")
  2. .lineLimit(3)

In SwiftUI, the above code shows output including 3 dots in the end.
Output:

  1. a
  2. b
  3. c...

But my target is to show the output without dots like this -
Target:

  1. a
  2. b
  3. c

答案1

得分: 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.

  1. Text("1\n 2 \n 3 \n 4 \n 5".truncateToLineLimit(3))
  1. extension String {
  2. func truncateToLineLimit(_ lineLimit: Int) -> String {
  3. var truncatedString = ""
  4. let lines = self.components(separatedBy: "\n").prefix(lineLimit)
  5. for line in lines {
  6. truncatedString += line.trimmingCharacters(in: .whitespacesAndNewlines)
  7. if line != lines.last {
  8. truncatedString += "\n"
  9. }
  10. }
  11. return truncatedString
  12. }
  13. }

huangapple
  • 本文由 发表于 2023年6月2日 13:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定