简体   繁体   English

iOS-sizeWithFont问题

[英]iOS - sizeWithFont issue

I have text "Testing of application". 我有文字“应用程序测试”。 it looks line in cell is 它在单元格中看起来是

"Tesing of 
 Application"

I want to add a Asteric right a text. 我想在文本上添加一个Asteric。 for eg 例如

"Tesing of *
Application"

but it show after the width of Label.. I have written the following code. 但是它显示在Label的宽度之后。.我编写了以下代码。

CGSize maximumLabelSize = CGSizeMake(99999,9999);

CGSize textSize = [[_label text] sizeWithFont:_label.font
                                 constrainedToSize:maximumLabelSize                                          lineBreakMode:_label.lineBreakMode];


float startX = 2 + textSize.width;
label2.frame = CGRectMake(startX, label2.frame.origin.y, label2.frame.size.width, label2.frame.size.height);

When you use constrainedToSize you have to pass the size with real height or real width. 使用constrainedToSize ,必须使用实际高度或实际宽度传递尺寸。

Let say you have a label on 300px width and you want to make the height according to text length you have to use CGSizeMake(300,9999) . 假设您有一个宽度为300px的标签,并且要根据文本长度设置高度,您必须使用CGSizeMake(300,9999) In this case the constrainedToSize method will return you the height need to draw that text. 在这种情况下, constrainedToSize方法将返回您绘制该文本所需的高度。

For getting the width you can set a small height like 20px to get the width needed to draw the text. 为了获得宽度,您可以设置一个小的高度(如20px以获取绘制文本所需的宽度。

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

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