以编程方式创建UILabel

jac*_*300 32 objective-c uilabel ios

我知道这应该很简单,但我一直在努力以编程方式创建UILabel并让它按照我想要的方式运行.

我想要的是能够创建一个标签,设置最大属性,高度,宽度和字体大小,然后让文本变小和/或只是截断文本以容纳长文本字符串.

假设我希望我的标签的文本最大宽度为380,最大高度为20,最大字体大小为12.

所以这就是我试图创建这样一个标签:

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];
Run Code Online (Sandbox Code Playgroud)

好的,所以标签出现了,但文字大于12,高度总是21!即使我将高度和文本大小值更改为极限大小,此代码也会创建一些无法调整的固定大小的标签.我似乎唯一可以缩小的是宽度.

我一定是在寻找一些基本的东西,但我真的无法弄清楚如何得到我想要的结果,也不明白为什么我得到了我得到的行为.

Ner*_*tel 54

以下工作如何?

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];
Run Code Online (Sandbox Code Playgroud)

编辑:我相信你可能会遇到使用adjustsFontSizeToFitWidth和的问题minimumScaleFactor.前者声明你还需要设置一个minimumFontWidth(根据我的测试,它可能会收缩到一些非常难以理解的东西),但是这个被弃用并被后者取代.

编辑2:没关系,过时的文档.adjustsFontSizeToFitWidth需要minimumScaleFactor,只要确保不将0作为minimumScaleFactor传递(整数除法,10/12返回0).baselineAdjustment价值变化也很小.


小智 13

UILabel *lbl1 = [[UILabel alloc] init];
/*important--------- */lbl1.textColor = [UIColor blackColor];
[lbl1 setFrame:position];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=NO;
lbl1.text= @"TEST";
[self.view addSubview:lbl1];
Run Code Online (Sandbox Code Playgroud)


kru*_*inh 7

这里是如何以编程方式创建UILabel ..

1)将其写入项目的.h文件中.

UILabel *label;
Run Code Online (Sandbox Code Playgroud)

2)将其写入项目的.m文件中.

label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];//Set frame of label in your viewcontroller.
[label setBackgroundColor:[UIColor lightGrayColor]];//Set background color of label.
[label setText:@"Label"];//Set text in label.
[label setTextColor:[UIColor blackColor]];//Set text color in label.
[label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
[label setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];//Set line adjustment.
[label setLineBreakMode:NSLineBreakByCharWrapping];//Set linebreaking mode..
[label setNumberOfLines:1];//Set number of lines in label.
[label.layer setCornerRadius:25.0];//Set corner radius of label to change the shape.
[label.layer setBorderWidth:2.0f];//Set border width of label.
[label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
[label.layer setBorderColor:[UIColor blackColor].CGColor];//Set Border color.
[self.view addSubview:label];//Add it to the view of your choice. 
Run Code Online (Sandbox Code Playgroud)