2012-02-07 333 views
27

我試過在線搜索,但沒有找到明確的答案,因此我來請求您的專家建議。我有一個視圖上有2個標籤。第一個標籤用於顯示名稱的縮寫,2-3個字母。第二個標籤顯示全名。ios動態尺寸標籤

我的問題是,如果有一種方法根據給定的字體類型,大小和字符串長度動態調整標籤的大小?我問,因爲我希望第二個標籤貼近第一個標籤,兩個標籤之間沒有太多的空白區域,或者沒有第一個標籤與第二個標籤重疊。

之所以這不是全部在一個標籤是因爲第一個標籤應該有一個更大的字體和不同的配色方案,然後第二個標籤。

任何意見非常感謝。

+0

請點擊此鏈接[搶mayoff的答案] [1] 由於 [1]:http://stackoverflow.com/questions/13206113/how-to-programmatically-change-a-uiviews-origin-as-labeled-in-xcode – umakanta 2013-08-07 05:44:22

回答

63

可以計算的,其中大小的字符串就會出現,然後可以設置您的UILabel該尺寸的框架看下面的代碼作爲樣本 -

//Calculate the expected size based on the font and linebreak mode of your label 
CGSize maximumLabelSize = CGSizeMake(296,9999); 

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
         constrainedToSize:maximumLabelSize 
         lineBreakMode:yourLabel.lineBreakMode]; 

//adjust the label the the new height. 
CGRect newFrame = yourLabel.frame; 
newFrame.size.height = expectedLabelSize.height; 
yourLabel.frame = newFrame; 

更新 -

使用而不是sizeWithAttributes:,現在需要NSDictionary。通過在對與關鍵UITextAttributeFont和您的字體對象是這樣的:

CGSize size = [string sizeWithAttributes: 
         @{NSFontAttributeName: 
         [UIFont systemFontOfSize:17.0f]}]; 

檢查Replacement for deprecated sizeWithFont: in iOS 7?更多細節

+1

你是先生是救世主。我仍然在努力學習iOS API以及所有東西都應該如何工作。 – Seb 2012-02-07 18:21:09

+0

@Seb - 祝你好運!快樂編程! – Saurabh 2012-02-07 18:23:05

+0

這部分代碼:lineBreakMode:yourLabel.lineBreakMode不適用於IOS 6 – Nazir 2013-04-11 12:42:51

16

這也將這樣的伎倆,並會考慮歸因文字

label.attributedText = attrString; 
CGSize maximumLabelSize = CGSizeMake(187,CGFLOAT_MAX); 
CGSize requiredSize = [label sizeThatFits:maximumLabelSize]; 
CGRect labelFrame = label.frame; 
labelFrame.size.height = requiredSize.height; 
label.frame = labelFrame; 
+0

這個解決方案更合理。 'UILabel'應該負責大小,而不是'NSString'。 – Berik 2013-02-21 11:26:55

+0

什麼是** CGFLOAT_MAX ** ?? – swiftBoy 2013-06-06 15:28:42

+0

這是浮點變量可以存儲的最高值。 – noRema 2013-06-12 10:45:46

7

除了Saurabh的回答。我有同樣的問題,你應該添加這條線
[yourLabel setNumberOfLines:0];
爲了整個文本顯示在X行。

+0

是的,在設置numberOfLines = 0之後,我的問題得到了解決,否則在實施Saurabh的解決方案時,我再次敲響了我的頭。 – Ans 2013-09-01 09:40:49

10

'sizeWithFont:'已棄用:iOS 7.0中不推薦使用。

所以儘量sizeWithAttributes

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 300, 20)]; 

    label.backgroundColor = [UIColor blueColor]; 

    const CGFloat fontSize = 30; 
    UIFont *regularFont = [UIFont systemFontOfSize:fontSize]; 
    UIColor *foregroundColor = [UIColor blackColor]; 

    attrs = [NSDictionary dictionaryWithObjectsAndKeys: 
    regularFont, NSFontAttributeName, 
    foregroundColor, NSForegroundColorAttributeName 
    , nil]; 

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] 
    initWithString:@"Test Text" 
    attributes:attrs]; 

    [label setAttributedText:attributedText]; 
    [self.view addSubview:label]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGSize expectedLabelSize = [label.text sizeWithAttributes:attrs]; // iOS 7 Code <-- 

    CGRect newFrame = label.frame; 
    newFrame.size.width = expectedLabelSize.width; 
    label.frame = newFrame; 

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] 
               initWithString:@"Longer Test Text" 
               attributes:attrs]; 

    [label setAttributedText:attributedText]; 
} 
8

由於sizeWithFont:在iOS的7已被棄用,你需要使用sizeWithAttributes(如maver解釋here)。爲了簡化代碼,你可以添加你可以重複使用這樣的方法:

-(CGRect)rectForText:(NSString *)text 
      usingFont:(UIFont *)font 
     boundedBySize:(CGSize)maxSize 
{ 
    NSAttributedString *attrString = 
     [[NSAttributedString alloc] initWithString:text 
             attributes:@{ NSFontAttributeName:font}]; 

    return [attrString boundingRectWithSize:maxSize 
            options:NSStringDrawingUsesLineFragmentOrigin 
            context:nil]; 
} 

,並利用它

CGSize maximumLabelSize = CGSizeMake(280,9999); 
UIFont *font = [UIFont systemFontOfSize:20]; 
CGRect titleRect = [self rectForText:post.title // <- your text here 
          usingFont:font 
         boundedBySize:maximumLabelSize];