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

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

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
    }
}

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:

确定