0

我正在使用循环 UILabels从数组动态创建。for

每个UILabel都有不同的长度。当我创建 时UILabels,它们彼此重叠。

我想在没有任何重叠的情况下UILabels正确安排它们。UIView有什么帮助吗?

4

5 回答 5

2

你可以在这里找到它是如何工作的:排列标签来源链接

  1. 使用网站上的相应按钮下载源代码
  2. 打开 ArrangeLabels 项目
  3. 打开ViewController.m文件
  4. 检查 -createElements -fitElements来自来源的方法。

如果您愿意,可以立即在浏览器中查看源代码:link to code

于 2013-08-06T10:35:40.987 回答
2

您可以使用sizeToFitnumberOfLines=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;


    }
于 2013-08-06T10:04:55.927 回答
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.
于 2013-08-06T10:34:56.050 回答
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;
}
于 2013-08-06T09:57:14.600 回答
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];
}
于 2013-08-06T10:03:50.903 回答