以编程方式创建UILabel [英] Programmatically Creating UILabel
问题描述
我知道这应该很简单,但是我一直在努力以编程方式创建UILabel并使它以我想要的方式工作.
I know this should be very simple, but I've been really struggling with creating a UILabel programmatically and getting it to behave the way I would like.
我想要做的就是能够创建标签,设置最大属性,包括高度,宽度和字体大小,然后使文本变小和/或仅截断文本以容纳较长的字符串.
All I want is to be able to create a label, set the maximum attributes as far as height, width and font size, and then have the text get smaller AND/OR just truncate the text to accommodate long strings of text.
假设我希望标签上的文字最大宽度为380,最大高度为20,最大字体为12.
Let's say that I want my label to have text that has a maximum width of 380, a maximum height of 20 and a maximum font size of 12.
这就是我试图创建这样的标签的步骤:
So here is what I have attempted to do to create such a label:
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 0, 0)];
fromLabel.text = [self fromSender];
fromLabel.font = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = YES;
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.size = [fromLabel.text sizeWithFont:fromLabel.font constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];
fromLabel.minimumScaleFactor = MIN_SCALE_FACTOR;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];
好吧,所以出现了标签,但文本大于12,高度始终显示为21 !!即使我将高度和文本大小值更改为极限大小,此代码也会创建一些无法调整的固定大小的标签.我似乎唯一可以减小的是宽度.
Ok, so the label appears, but the text is larger than 12 and the height always comes out to be 21!? Even if I change the height and text size values to extreme sizes, this code creates a label of some fixed size that can't be adjusted. The only thing I can seem to make smaller is the width.
我必须研究一些基本的知识,但是我真的无法弄清楚如何获得理想的结果,也无法理解为什么我会得到自己正在得到的行为.
I must be over looking something basic, but I really can't figure out how to get my desired result, nor do I understand why I'm getting the behavior that I am getting.
推荐答案
以下工作吗?
UIFont * customFont = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
NSString * text = [self fromSender];
CGSize labelSize = [text sizeWithFont:customFont constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
fromLabel.text = text;
fromLabel.font = customFont;
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];
edit:我相信您同时使用adjustsFontSizeToFitWidth
和minimumScaleFactor
可能会遇到问题.前者指出,您还需要设置一个minimumFontWidth
(否则,根据我的测试,它可能会缩小到非常不可读的位置),但已弃用并由后者取代.
edit : I believe you may encounter a problem using both adjustsFontSizeToFitWidth
and minimumScaleFactor
. The former states that you also needs to set a minimumFontWidth
(otherwhise it may shrink to something quite unreadable according to my test), but this is deprecated and replaced by the later.
不要紧,过时的文档. adjustsFontSizeToFitWidth
需要minimumScaleFactor
,只需确保不将其作为minimumScaleFactor传递0(整数除法,10/12返回0).
baselineAdjustment
值也有小的变化.
edit 2 : Nevermind, outdated documentation. adjustsFontSizeToFitWidth
needs minimumScaleFactor
, just be sure no to pass it 0 as a minimumScaleFactor (integer division, 10/12 return 0).
Small change on the baselineAdjustment
value too.
这篇关于以编程方式创建UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!