【问题标题】:Get the end of a string in a uitextview在 uitextview 中获取字符串的结尾
【发布时间】:2015-09-14 13:13:12
【问题描述】:

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

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

所以,我想要这样的东西:

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

谢谢你,

【问题讨论】:

  • 其实这是自定义UITableViewCell不是UITextField
  • 是的,但在我的情况下,它是类似的,不是吗?我有一个 UITableViewCell,里面有一个 UITextView。

标签: ios height uilabel uitextview xib


【解决方案1】:

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

UITextPosition *Pos2 = [textView positionFromPosition:textView.endOfDocument offset:0];
UITextPosition *Pos1 = [textView positionFromPosition:textView.endOfDocument offset:-1];
UITextRange *range = [textView textRangeFromPosition:Pos1 toPosition:Pos2];
CGRect lastCharRect = [textView firstRectForRange:(UITextRange *)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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多