2012-07-09 4 views
1

UILabel의 프레임 크기에 맞도록 UIFont 크기를 자동 크기 조정하는 다중 행 UILabel을 사용할 수 있습니까? 그렇지 않다면 이것을 달성하는 가장 좋은 방법은 무엇입니까? 나는 UILabel/UITextView의 프레임 크기를 조정하고 싶지 않습니다. 지금까지 내가 이런 식으로 해왔 :여러 줄 자동 크기 조정 텍스트 UILabel

int currentFontSize = 18; 
    UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize]; 
    CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize]; 

    while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){ 
     currentFontSize--; 
     currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize]; 
     storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize]; 
    } 

    [self.newsfeedStoryTitle_ setFont:currentFont]; 
    [self.newsfeedStoryTitle_ setText:storyTitle]; 

답변

3

당신이 원하는 : sizeWithFont:constrainedToSize:lineBreakMode:[Source]

//Calculate the expected size based on the font and linebreak mode of your label 
CGSize maximumLabelSize = CGSizeMake(self.bounds.size.width, NSIntegerMax); 

CGSize expectedLabelSize = [theString sizeWithFont:the Font constrainedToSize:maximumLabelSize lineBreakMode:theLineBreakMode]; 

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