iOS 计算某一控件里面文字占的宽高

自从iOS7以后更新了计算方法

    //定义字体
    NSDictionary *attrs = @{NSFontAttributeName :[UIFont systemFontOfSize:14]};
    
    //定义这个控件文字最大的宽度和高度,这里定义了可以无限宽和高
    CGSize maxsizes=CGSizeMake(MAXFLOAT, MAXFLOAT);
     
    UILabel *texts;
    texts.text=@"haaaaaaaaa";
    //textzise即为当前控件文字的所占宽高
    CGSize textzise= [texts.text boundingRectWithSize:maxsizes options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    NSLog(@"%f--%f",textSize.width,textzise.height);

  

posted @ 2016-01-13 10:03  欲眠  阅读(175)  评论(0编辑  收藏  举报