1

I have found this solution how to fit UILabel height to its content. It works perfect for example if I follow suggested instruction and modify vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority. So then storyboard automatically fit UILabel height to text height. If I make the same for horizontal ContentHuggingPriority and ContentCompressionResistancePriority and edit UILabel width constraint priority, UILabel fits width to text width.

My question is how to set parameters to fit size as for text height and as for text width? Because seems we can only fit or hight or width of UILabel content.

Community
  • 1
  • 1
Matrosov Alexander
  • 20,713
  • 42
  • 130
  • 259
  • You mean maximum width and height that can be used? – Wain Dec 15 '14 at 12:03
  • @Wain, I mean that that explanation (link with Marks answer I've shared) just display how to adjust fit to content height, but I need fit to height and width also. – Matrosov Alexander Dec 15 '14 at 12:37
  • 1
    What do you want to achieve? `UILabel` can grow either by adding additional lines of text or by expanding its width (but at some point it won't be able to fit on the screen). You have to specify which behavior you want by using `numberOfLines` / height constraint or by setting `preferredMaxLayoutWidth`. – Michał Ciuba Dec 15 '14 at 12:48

1 Answers1

0
+(CGSize)findTextHeight:(float)height width:(float)width string:(NSString *)desc font:(UIFont*)font{
    if ([desc isKindOfClass:[NSNull class]]) {
        return 0.0;
    }
    CGRect textRect =[desc boundingRectWithSize:CGSizeMake(width, height)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName:font}
                                        context:nil];
    CGSize size = textRect.size;
    return size;
}

This will gives you the label's size from given text and minimum height and minimum width.

Sudhin Davis
  • 1,932
  • 11
  • 17