可可数组到字符串被截断

发布于 2024-08-17 02:01:16 字数 321 浏览 3 评论 0原文

我正在尝试将对象数组连接到一个字符串中,该字符串最终将用于填充表格单元格的详细信息标签。如果文本太长,它会自动截断并在末尾添加一个省略号。

我想通过检查字符串是否少于 40 个字符并在中间添加一个省略号来避免这种情况。

因此,如果我的数组是“一”、“二”、“三”、“四”和“五”,假设它们加起来超过 40 个字符,并且我的分隔符是“>”,那么它可能看起来像“一 >”两个 > 三 > 四...”,如果它被自动截断。

相反,我希望它看起来像“一>二>...>五”,所以我总是至少数组中的第一个和最后一个项目。

有什么想法吗?

I'm trying to join an array of objects into a string that will ultimately be used to populate the detail label of a table cell. if the text is too long it will automatcally truncate and add an elipse at the end.

I want to avoid this by checking that the string is less than say 40 characters and adding an elipse in the middle instead.

so if my array is "one", "two", "three", "four" and "five" assuming they added up to more than 40 characters and my separator is ">", it may look like "one > two > three > four..." if it were automatically truncated.

Instead I want it to look like "one > two >... > five" so I always at least the first and last item from the array.

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-08-24 02:01:16

我只需使用 UILabel 并将其 lineBreakMode 设置为 UILineBreakModeMiddleTruncation。然后只需使用 -[NSArray arrayComponentsJoinedByString:] 将元素连接在一起。

I would just use a UILabel and set its lineBreakMode to UILineBreakModeMiddleTruncation. Then just use -[NSArray arrayComponentsJoinedByString:] to join the elements together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文