2010-11-04 126 views

回答

106

这里假设你已经设置字体:

label.text = @"some text"; 
[label sizeToFit]; 

您还需要定义一个最大宽度,并告诉你的程序如果sizeToFit给你的宽度大于最大值,该怎么做。

+3

sizeToFit方法是UIView的一种方法。 – TopChul 2011-08-16 04:03:44

+53

如何定义最大宽度? – 2012-08-07 13:44:42

+0

+1这个答案帮了我最大的忙。 – 2012-10-23 06:17:38

7

我在这里看到三个选项。

首先,让标签的尺寸足够大以容纳任何文字。这很简单,但并不总是很好 - 取决于其周围的景观。

二,标签可以调整较长文本的字体大小(adjustsFontSizeToFitWidth属性)。这通常是不可取的,元素中的不同字体可能看起来很丑。

最后一个选项是根据当前保存的文本以编程方式调整标签的大小。为了计算与当前字体使用像这样保存文本所需的大小:

CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap]; 
+0

如果我们使用UILineBreakModeWordWrap我们可以进入上的UILabel多行,我怎么可以设置两条线之间的空行。 – 2012-07-04 09:40:58

21

下面是如何做到这一点,假设以下messageLabel是您想要达到预期效果的标签。现在,尝试代码的这些简单的行:

// Set width constraint for label; it's actually the width of your UILabel 
    CGFloat constrainedWidth = 240.0f; 
    // Calculate space for the specified string 
    CGSize sizeOfText = [yourText sizeWithFont:yourFont constrainedToSize:CGSizeMake(constrainedWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,constrainedWidth,sizeOfText.height)]; 
    messageLabel.text = yourText; 
    messageLabel.numberOfLines = 0;// This will make the label multiline 
+7

这实际上是一个很好的答案......只是不是针对这个特定的问题!如果您想在维持固定宽度的同时自动调整“UILabel”的*高度*,此答案将很有效。事实上,当我看到这个页面时,我正在寻找这个东西。我很乐意为您的答案投票...但它不回答问题。不过,好的解决方案,可能会帮助其他人绊倒这个页面。 – 2012-04-23 15:23:52

+1

谢谢jon!其实我也在寻找这个解决方案,顺便说一句,我设法做到了,然后认为我应该在这里分享它。 :)顺便说一句,那个时候发生的事情是,我没有很好地注意到这个问题,所以犯了错误,我想。对不起,很高兴它帮助你:D – 2012-04-23 16:53:47

+0

嗯,它帮助我投票。 – 2012-10-25 11:26:26

12
NSString *[email protected]"I am here."; 
CGSize stringsize1 = [txt1 sizeWithFont:[UIFont systemFontOfSize:14]]; 
[label setFrame:CGRectMake(x,y,stringsize1.width,hieght)]; 
[label setText:txt1]; 
1

关注这个。

CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]]; 
[label setFrame:CGRectMake(x,y,stringsize.width,height)]; 
[label setText: yourString]; 
+0

此方法已弃用 – 2016-05-19 14:54:08

2

如果设置的字体和大小已经,如果你有你的框架定义,请尝试使用这两种常见情况如下:

if (label.text.length > maxCharPerLine) [label setNumberOfLines:0]; // infinite lines 
else [label setNumberOfLines:1]; // one line only 

// Adjust your font size to fit your desired width. 
[label setAdjustsFontSizeToFitWidth:YES]; 
+0

假设您尝试多行标签,那么是的这是正确的方法。用于将课程设置的行数单行标记为1。 – 2017-03-07 18:19:37

1

由于sizeWithFont在IOS 7.0,那么你是贬值下面的代码

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) 



if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 
     // code here for iOS 5.0,6.0 and so on 
     CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
    } else { 
     // code here for iOS 7.0 
     fontSize = [itemCat_text sizeWithAttributes: 
          @{NSFontAttributeName: 
           [UIFont fontWithName:@"Helvetica" size:12]}]; 
    } 
1

使用自动布局:

enter image description here

在视图控制器:

override func viewDidLoad() { 
     super.viewDidLoad() 

self.sampleLabel.text = "Electrical Maintenance and Repair" 
      self.sampleLabel..sizeToFit() 

} 
相关问题