OGeek|极客世界-中国程序员成长平台

标题: ios - 在 uitextview 中获取字符串的结尾 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:44
标题: ios - 在 uitextview 中获取字符串的结尾

我想创建一个消息框,就像 Telegram 和其他聊天一样,在框的底部有时间。

但如果文本在行尾结束,时间标签应该在下一行,否则,应该在同一行。

所以,我想要这样的东西: new line for date when content is goes until the end

如何在我的 xib 中或以编程方式完成此操作?

谢谢你,



Best Answer-推荐答案


您可以计算 TextView 中最后一个字符的位置并检查是否有足够的空间放置时间标签然后显示它,否则将其添加到新行。

UITextPosition *Pos2 = [textView positionFromPosition:textView.endOfDocument offset:0];
UITextPosition *Pos1 = [textView positionFromPosition:textView.endOfDocument offset:-1];
UITextRange *range = [textView textRangeFromPositionos1 toPositionos2];
CGRect lastCharRect = [textView firstRectForRangeUITextRange *)range];
CGFloat freeSpace = textView.frame.size.width - lastCharRect.origin.x - lastCharRect.size.width;
if (freeSpace > requiredWidthForTimeLabel) {
    //add time label to the last line     
} else {
    //add time label on a new line
}

关于ios - 在 uitextview 中获取字符串的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31092546/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4