简体   繁体   English

将根视图宽度设置为等于动态标签的宽度

[英]Set root view width equal to dynamic label's width

I'm using XIB to populate tableview of a chat app (messages). 我正在使用XIB填充聊天应用程序的表视图(消息)。 It contains a view and a label on it. 它包含一个视图和一个标签。 The label's text continuously changes. 标签的文字不断变化。 How can I find the width of label and then set width of parent view equal to that? 如何找到标签的宽度,然后将父视图的宽度设置为等于该宽度?

I'm a newbie to swift. 我是个新手。 So, in this way, is it possible to set height also? 因此,以此方式也可以设置高度吗?

I have set the number of lines to '0' of the uilabel 我已将uilabel的行数设置为“ 0” 在此处输入图片说明

Assuming that the "Message Text" label is the only subview within the parent view, you could set the parent view's height and width like so: 假设“消息文本”标签是父视图中的唯一子视图,则可以这样设置父视图的高度和宽度:

if let subview = view.subviews.first {
    // where `view` is your parent view and `subview` is your text label
    view.frame.size.width = subview.frame.width
    view.frame.size.height = subview.frame.height  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM