我正在使用循环 UILabels
从数组动态创建。for
每个UILabel
都有不同的长度。当我创建 时UILabels
,它们彼此重叠。
我想在没有任何重叠的情况下UILabels
正确安排它们。UIView
有什么帮助吗?
你可以在这里找到它是如何工作的:排列标签来源链接
ViewController.m
文件createElements
-fitElements
来自来源的方法。如果您愿意,可以立即在浏览器中查看源代码:link to code
您可以使用sizeToFit
和numberOfLines=0
int y=0;
for (int k=0; k<[array count]; k++)
{
UILabel *lb=[[UILabel alloc]initWithFrame:CGRectMake(5 , y, 100,60)];
lb.textAlignment=UITextAlignmentLeft;
lb.text=[array objejectAtIndex:k];
lb.numberOfLines=0;
[lb sizeToFit];
[self.view addSubview:lb];
y+=lb.frame.size.height;
}
Try to implement this logic:
-(void)adjustLabel1Text1:(NSString *)text1
{
UILabel *lbl_first = [UIFont fontWithName:@"Helvetica" size:12];
text1 = [text1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
float hedingLblHeight = [self calculateHeightOfTextFromWidth:text1 : [UIFont fontWithName:@"Helvetica" size:12] :118 :UILineBreakModeWordWrap];
lbl_first.text=text1;
[lbl_first setFrame:CGRectMake(lbl_first.frame.origin.x, lbl_first.frame.origin.y, 118, hedingLblHeight)];
lbl_first.lineBreakMode = UILineBreakModeWordWrap;
lbl_first.numberOfLines = 0;
[lbl_first sizeToFit];
//////////Adjust the lable or any UIControl below this label accordingly.
float endResultHeight=[self calculateHeightOfTextFromWidth:text2 : [UIFont fontWithName:@"Helvetica" size:15] :299 :UILineBreakModeWordWrap];
if(hedingLblHeight>secondImgTitleHeight)
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
else
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
lbl_endResult.lineBreakMode=UILineBreakModeWordWrap;
lbl_endResult.numberOfLines = 0;
[lbl_endResult sizeToFit];
}
-(float) calculateHeightOfTextFromWidth:(NSString*)text : (UIFont*) withFont:(float)width :(UILineBreakMode)lineBreakMode
{
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
return suggestedSize.height;
}
It has helped me a lot.Hope it works for you.
您需要像这样更改标签的 y 坐标
UILabel *hiLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,10,50,20)];
hiLbl.text = @"Hi";
[self.view addSubview:hiLbl];
UILabel *hellolbl = [[UILabel alloc] initWithFrame:CGRectMake(0,30,50,20)];
hellolbl.text = @"Hi";
[self.view addSubview:hellolbl];
或者
UILabel *lbl;
for(int i = 1 ; i < 3 ; i++)
{
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,i*10+10,50,20)];
[self.view addSubview:lbl];
lbl.text = i;
}
没有任何屏幕截图,很难猜出您指的是哪种类型的重叠。并且长度UILabel
没有帮助,无论是指定宽度还是高度。
但是,如果您在谈论 的宽度UILabel
,那么这段代码可能会对您有所帮助。
让您拥有三个不同宽度的标签,它们位于宽度数组的数组中:{ 10, 20, 30}。
float width = [array objectAtIndex:0];
float x = 0.of;
float padding = 10.0f;
for(int i =0; i<3; i++){
UILabel *label = [UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview: label];
x = x + width + paddng;
width = [array objectAtIndex:i + 1];
}