[英]UITextView dynamic width according to text inside in UITableViewCell
我正在努力使聊天气泡像在iMessage中一样。 我必须使用UITextView来利用DataDetectors来自动检测URL,电话号码和地址。 我的视图层次结构就像;
-UITableViewCell
-ContentView
-ChatBubbleContainerView
-UITextView
ChatBubbleContainerView
具有以下约束
ContainerView
UITextView具有以下约束
ChatBubbleContainerView
我想实现一种行为,使文本视图根据其内部内容(例如文本)自动扩展其高度和宽度。 但是文本视图没有固有的内容大小 。
这些是我尝试过并得到错误结果的
intrinsicContentSize
属性,但是它也不起作用。 ChatBubbleContainerView
一个宽度约束,并将其更改为sizeThatFits()
方法的结果 ChatBubbleContainerView
一个宽度约束,并将其更改为text.boundingRect
方法的结果 这些是我得到的结果
当我使用UILabel
尝试相同的布局时,它按预期工作。 如您所知, UILabel
具有固有的内容大小。
谢谢
TextView约束很好,您需要对ChatBubbleContainerView约束进行一些更改。
ChatBubbleContainerView约束:
顶部,顶部,底部-> 10点
将MaxWidth(GreaterThanOrEqual)设置为contentView,例如0.85 ,也可以将TrailingConstraint设置为maxConstant(GreaterThanOrEqual设置为contentView。
您需要设置maxWidth,然后只有ChatBubbleContainerView会响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.