Lable 的宽 高自适应

来源:互联网 发布:如何看淘宝消费总额 编辑:程序博客网 时间:2024/04/28 07:55


   UILabel *labcurrent=[[UILabelalloc]initWithFrame:CGRectMake(0,2,18, 18)];

    labcurrent.text=@"blabla;

    labcurrent.font=[UIFontsystemFontOfSize:14.0];

    labcurrent.backgroundColor=[UIColorclearColor];

   CGSize sizeWidth=[labcurrent.textsizeWithFont:labcurrent.fontconstrainedToSize:CGSizeMake(MAXFLOAT,26)];

   CGSize sizeHeight=[labcurrent.textsizeWithFont:labcurrent.fontconstrainedToSize:CGSizeMake( sizeWidth.width,MAXFLOAT)];

    [labcurrentsetFrame:CGRectMake(8,13,sizeWidth.width , sizeHeight.height)];

   注;如果想要lable的宽度自适应,那么只需要把lable的宽度改成:CGSizeMake(MAXFLOAT26);

     反之,要高度自适应,就把lable的高度改成:CGSizeMake( 30,MAXFLOAT)];

 创建lable 之初要给个frame ,自适应之后再设一个frame,就可以重设lable的frame;

原创粉丝点击