可可数组到字符串被截断
我正在尝试将对象数组连接到一个字符串中,该字符串最终将用于填充表格单元格的详细信息标签。如果文本太长,它会自动截断并在末尾添加一个省略号。
我想通过检查字符串是否少于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只需使用
UILabel
并将其lineBreakMode
设置为UILineBreakModeMiddleTruncation
。然后只需使用-[NSArray arrayComponentsJoinedByString:]
将元素连接在一起。I would just use a
UILabel
and set itslineBreakMode
toUILineBreakModeMiddleTruncation
. Then just use-[NSArray arrayComponentsJoinedByString:]
to join the elements together.