我使用UILabels
循环从数组动态创建for
。
每个UILabel
都有不同的长度。当我创建UILabels
时,它们彼此重叠。
我想在UILabels
内正确排列这些UIView
而不重叠。有什么帮助吗?
答案 0 :(得分:2)
您可以使用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;
}
答案 1 :(得分:2)
您可以在此处找到它的工作原理:arrange labels sources link
ViewController.m
档案createElements
-fitElements
方法。如果您愿意,可以立即在浏览器中查看来源:link to code
答案 2 :(得分:1)
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.
答案 3 :(得分:0)
您需要像这样更改标签的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;
}
答案 4 :(得分:0)
如果没有任何屏幕截图,很难猜出您所指的重叠的类型。 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];
}