Objective c 多行自动调整文本标签

Objective c 多行自动调整文本标签,objective-c,ios,uilabel,Objective C,Ios,Uilabel,是否可以使用多行UILabel自动调整UIFont大小,使其适合UILabel的框架大小?如果没有,那么实现这一目标的最佳方式是什么?我不想将UILabel/UITextView的帧大小调整为任何大小。到目前为止,我一直在这样做: int currentFontSize = 18; UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize]; CGSize storyTitle

是否可以使用多行UILabel自动调整UIFont大小,使其适合UILabel的框架大小?如果没有,那么实现这一目标的最佳方式是什么?我不想将UILabel/UITextView的帧大小调整为任何大小。到目前为止,我一直在这样做:

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];

您需要:
sizeWithFont:constrainedToSize:lineBreakMode:

//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;