简体   繁体   English

使用自动布局的自定义/动态标签宽度

[英]Custom/Dynamic label width using auto layout

I have a table cell on which I presently have a UILabel say labelA whose height is dynamic ie based on the content. 我有一个表格单元格,当前有一个UILabel说labelA,它的高度是动态的,即基于内容。 The label width is 290px. 标签宽度为290像素。 I wish to add another label next to labelA whose name is say labelB. 我希望在名称为labelB的labelA旁边添加另一个标签。 The table cell and labelA looks as per below: 表格单元格和labelA如下所示: 在此处输入图片说明

Question 1 - Here the height is expanding and I have 2 rows to display the entire text here. 问题1-高度在扩大,我有2行在这里显示整个文本。 But it looks like width for the 1st and 2nd row is same as 290. Is there a way I can stop the width of the label where my text ends ie in this case the width of my label for 1st row should be 290 but for 2nd row it should end after the last work "Paul" so that I can start my next label labelB from there? 但是看起来第一行和第二行的宽度与290相同。有没有办法我可以在文本结尾处停止标签的宽度,即在这种情况下,第一行的标签宽度应该是290,而第二行行应该在最后一个工作“ Paul”之后结束,以便我可以从那里开始下一个标签labelB?

Question 2 - Also, while creating the xib, I am placing both my labels on the same row. 问题2-同样,在创建xib时,我将两个标签都放在同一行上。 If I set a constraint between both the labels, can labelA push labelB down to another row when it expands? 如果我在两个标签之间都设置了约束,则labelA可以在扩展时将labelB向下推到另一行吗?

My cell and labels finally should look as below where the label with purple background is labelA and rect with orange border and white solid color inside is labelB. 我的单元格和标签最终应如下所示,其中紫色背景的标签为labelA,矩形边框为橙色且内部为白色纯色的矩形为labelB。

在此处输入图片说明

Please let me know if this is possible? 请让我知道是否可行? I am still a noob in iOS and auto layout. 我仍然是iOS和自动版式设计的菜鸟。

To answer Question 1 , UILabel will always draw text into a rectangular frame. 为了回答问题1UILabel始终将文本绘制到矩形框架中。 You can't have one UILabel draw two lines of text the way you're looking for. 您不能让一个UILabel按照您想要的方式绘制两行文本。 You could use two UILabel s, one for each line, but you'd have to do some manual calculations to figure out how much text will fit in the first label, and then what text to "overflow" to the second ( boundingRectWithSize:options:attributes:context: would be your friend for this). 您可以使用两个UILabel ,每行一个,但是您必须进行一些手动计算以找出第一个标签中可以容纳多少文本,然后找出哪些文本“溢出”到第二个标签( boundingRectWithSize:options:attributes:context:将是您的朋友)。

To answer Question 2 , you should pin the bottom edge of labelB to the bottom edge of labelA . 要回答第二个问题 ,你应约束labelB的底部边缘labelA的底部边缘。 Let the height of each label be determined by the intrinsic content size. 让每个标签的高度由内在内容的大小确定。 Assuming the font is the same, this should result in the result you want. 假设字体相同,则应得到所需的结果。 (You can also try aligning the two labels' baselines, but I can't recall how that behaves for multiline labels.) (您也可以尝试对齐两个标签的基线,但我不记得多行标签的行为。)

By the way, you should look into iOS 7's Text Kit . 顺便说一句,您应该研究一下iOS 7的Text Kit I personally haven't had a chance to dive into it in much depth yet, but it's very powerful and you might find a better way to do what you're after here. 我个人还没有机会更深入地研究它,但是它功能非常强大,您可能会找到一种更好的方法来做自己想做的事情。

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

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