Pixel Width of the text in a UILabel

轉載URL:http://stackoverflow.com/questions/1340667/pixel-width-of-the-text-in-a-uilabel


NSString has a sizeWithAttributes: method that can be used for this. It returns a CGSize structure, so you could do something similar to the following to find the width of the text inside your label.

iOS 7 and higher

CGSize textSize = [[label text] sizeWithAttributes:@{NSFontAttributeName:[label font]}];CGFloat strikeWidth = textSize.width;

iOS <7

Prior to iOS7, you had to use the sizeWithFont: method.

CGSize textSize = [[label text] sizeWithFont:[label font]];CGFloat strikeWidth = textSize.width;

UILabel has a font property that you can use to dynamically get the font details for your label as i'm doing above.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章