iPhone - Adjust UILabel width according to the text

后端 未结 7 1543
天涯浪人
天涯浪人 2020-12-28 08:17

How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to th

7条回答
  •  囚心锁ツ
    2020-12-28 08:43

    //use this for custom font
    CGFloat width =  [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;
    
    //use this for system font 
    CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;
    
    label.frame = CGRectMake(point.x, point.y, width,height);
    
    //point.x, point.y -> origin for label;
    //height -> your label height; 
    

提交回复
热议问题