iPhone - 根据文字调整UILabel宽度 [英] iPhone - Adjust UILabel width according to the text
本文介绍了iPhone - 根据文字调整UILabel宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何根据文字调整标签宽度?如果文本长度很小我想要标签宽度小......如果文本长度很小我想要标签宽度根据文本长度。有可能吗?
How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to that text length. Is it possible?
实际上我有两个UI标签。我需要把这两个放在附近。但如果第一个标签的文字太小,就会有很大的差距。我想消除这个差距。
Actually I have Two UIlabels. I need to place these two nearby. But if the first label's text is too small there will be a big gap. I want to remove this gap.
推荐答案
//use this for custom font
CGFloat width = [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;
//use this for system font
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;
label.frame = CGRectMake(point.x, point.y, width,height);
//point.x, point.y -> origin for label;
//height -> your label height;
这篇关于iPhone - 根据文字调整UILabel宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文