UILabel auto resize on basis of text to be shown

后端 未结 10 2025
余生分开走
余生分开走 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:37

    The sizeToFit method worked just great.

    I did following.

    UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(6,3, 262,20 )]; // RectMake(xPos,yPos,Max Width I want, is just a container value);
    
    NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...";
    
    testLabel.text = test;
    testLabel.numberOfLines = 0; //will wrap text in new line
    [testLabel sizeToFit];
    
    [self.view addSubview:testLabel];
    

提交回复
热议问题