UILabel auto resize on basis of text to be shown

后端 未结 10 1894
余生分开走
余生分开走 2021-01-29 22:44

I\'m working on an application, in which I\'m required to autoresize the text area on basis of text to be displayed.

Firstly, I\'m not sure for this either I should use

10条回答
  •  抹茶落季
    2021-01-29 23:26

    You can find a text size with :

    CGSize textSize = [[myObject getALongText] 
                        sizeWithFont:[UIFont boldSystemFontOfSize:15] 
                        constrainedToSize:CGSizeMake(maxWidth, 2000)
                        lineBreakMode:UILineBreakModeWordWrap];
    

    then you can create your UILabel like that :

    UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,textSize.width, textSize.height];
    [lbl setNumberOfLines:0];
    [lbl setLineBreakMode:UILineBreakModeWordWrap];
    [lbl setText:[myObject getALongText]];
    

提交回复
热议问题