2016年1月6日 星期三

iOS tips - 動態設置label width

在設置一個UILabel時,常常會遇到設置的frame width不夠文字內容使用的問題,雖說也有adjustsFontSizeToFitWidth這個property可以設定,但是調整label字型大小通常不會是設計的選項之一.
此時我們可以先依靠"sizeWithFont"這個function來detect要帶入的文字的寬度,再去設置UILabel的frame
//use this for system font 
CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);
如此就能夠根據文字內容來動態定義Label的width了

沒有留言:

張貼留言