文字高度自適應

//自定義高度
//導致計算不準確:
   
 //1. 限定的寬度和lable 的寬度不一致
   
 //2. 計算時使用的字體及字號與lable 顯示時使用的字體和字號不一致
   
 NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont  systemFontOfSize:12], NSFontAttributeName, nil];
   
   
 //第一個參數: 寬度  lable 一樣, 高度要麼不設(0), 要麼就足夠大,否則都會導致計算錯誤
   
 //第二個參數: 是從原點開始繪製
   
   
 //第三個參數: 是文本繪製所需的屬性, :字號 
   
 CGRect rect = [str boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
   
 //NSLog(@"%@", NSStringFromCGRect(rect));
    aLable.
frame = CGRectMake(aLable.frame.origin.x , aLable.frame.origin.y, aLable.frame.size.width, rect.size.height);

6.0 計算字符串高度

CGSize rect = [lab sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(280,2000) lineBreakMode:NSLineBreakByTruncatingHead];

   
    7.0 計算字符串高度
    CGRect rect =  [str boundingRectWithSize:CGSizeMake(300, 800) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:Nil]; 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章