2013-09-26 58 views
4

在iOS 6中,我使用的:動態調整標籤在IOS 7

CGSize labelSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode]; 
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , labelSize.width, self.frame.size.height); 

動態調整一個UILabel。這並不在iOS的7工作,所以我嘗試:

NSString *text = self.text; 
CGFloat width = size.width; 
UIFont *font = self.font; 
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text 
                   attributes:@{ NSFontAttributeName: font }]; 

CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} 
           options:NSStringDrawingUsesDeviceMetrics 
           context:nil]; 
CGSize size = rect.size; 

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , size.width, self.frame.size.height); 

這是一個的UILabel類裏面,但是這是不是也工作... 任何想法,我應該使用?

+0

哪部分不工作?計算的框架尺寸是錯誤的,還是標籤尺寸更改沒有發生? – tarmes

回答

6

嘗試是這樣的(無自動佈局工作):

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                  [UIFont fontWithName:@"FontName" size:15], NSFontAttributeName, 
                  nil]; 

CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0) 
                options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:attributesDictionary 
                context:nil]; 

CGSize size = frame.size; 
3

如果沒有,爲什麼它不能正常工作更多的細節,我的猜測是,你需要使用的選項NSStringDrawingUsesLineFragmentOrigin,以便它要成爲一個簡易替換舊sizeWithFont:,像這樣:

NSString *text = ...; 
CGFloat width = ...; 
UIFont *font = ...; 
NSAttributedString *attributedText = 
    [[NSAttributedString alloc] 
     initWithString:text 
     attributes:@ 
     { 
      NSFontAttributeName: font 
     }]; 
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} 
              options:NSStringDrawingUsesLineFragmentOrigin 
              context:nil]; 
CGSize size = rect.size; 

請注意文檔中提到:

在iOS 7及更高版本中,此方法返回小數大小(返回的CGRect的大小爲 組件);要使用返回的大小來設置 的視圖,則必須使用ceil函數將其值提高到最接近的較大整數 。

於是拔出計算的高度或寬度用於上漿的意見,我會用:

CGFloat height = ceilf(size.height); 
CGFloat width = ceilf(size.width); 
+0

我試過這個...它不起作用... – user426132

+0

@ user426132幫我理解什麼是不工作的。什麼部分不是'sizeWithFont:'習慣的方式? –

1

這應該在iOS6的和iOS7工作,但會打破你的標籤的限制(需要設置他們都回來了編程如果需要的話):

-(void)resizeHeightForLabel: (UILabel*)label { 
    label.numberOfLines = 0; 
    UIView *superview = label.superview; 
    [label removeFromSuperview]; 
    [label removeConstraints:label.constraints]; 
    CGRect labelFrame = label.frame; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
     CGRect expectedFrame = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, 9999) 
                 options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                   label.font, NSFontAttributeName, 
                   nil] 
                 context:nil]; 
     labelFrame.size = expectedFrame.size; 
     labelFrame.size.height = ceil(labelFrame.size.height); //iOS7 is not rounding up to the nearest whole number 
    } else { 
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 
     labelFrame.size = [label.text sizeWithFont:label.font 
           constrainedToSize:CGSizeMake(label.frame.size.width, 9999) 
            lineBreakMode:label.lineBreakMode]; 
#pragma GCC diagnostic warning "-Wdeprecated-declarations" 
    } 
    label.frame = labelFrame; 
    [superview addSubview:label]; 
} 

添加這個方法將你的viewController,並使用它像這樣:

[self resizeHeightForLabel:myLabel]; 
//set new constraints here if needed 
+0

我需要調整寬度不是高度...我會給它一個去.. thx – user426132

+0

試試吧,我認爲修改代碼來調整寬度並不難。 –

+0

嘗試過......沒有工作 – user426132

3
- (void)resizeLabelByContent:(UILabel *)label 
{ 

    CGSize maxSize = CGSizeMake(label.width, 999); 

    NSString *contentStr = label.text; 

    UIFont *contentFont = label.font; 

    CGRect contentFrame; 

    NSString *version = [[UIDevice currentDevice] systemVersion]; 

    if ([version floatValue] < 7.0) { 

     CGSize contentStringSize = [contentStr sizeWithFont:contentFont constrainedToSize:maxSize lineBreakMode:label.lineBreakMode]; 

     contentFrame = CGRectMake(label.left, label.top, label.width, contentStringSize.height); 

    } else { 

     NSDictionary *contentDic = [NSDictionary dictionaryWithObjectsAndKeys:contentFont, NSFontAttributeName, nil]; 

     CGSize contentStrSize = [contentStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size; 

     contentFrame = CGRectMake(label.left, label.top, label.width, contentStrSize.height); 
    } 

    label.frame = contentFrame; 
}