Ios 如何创建uilabel';是从NSMutableArray动态生成的吗?

Ios 如何创建uilabel';是从NSMutableArray动态生成的吗?,ios,objective-c,uilabel,Ios,Objective C,Uilabel,NSMutableArray*项//包含15项 我需要把一个标签从另一个我尝试这样的东西,但没有工作 int count=20; for(int i = 0; i < [items count]; i++){ UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0,0,count)]; label.text = @"text"; //etc... count+=20;

NSMutableArray*项
//包含15项

我需要把一个标签从另一个我尝试这样的东西,但没有工作

int count=20;

for(int i = 0; i < [items count]; i++){
        UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,0,0,count)];
        label.text = @"text"; //etc...
        count+=20;

        [_scroll addSubview:label];

    }
int计数=20;
对于(int i=0;i<[项目计数];i++){
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,0,count)];
label.text=@“text”;//等。。。
计数+=20;
[_scrolladdsubview:label];
}

我能做些什么呢谢谢我想你是想改变帧的Y值,但是CGRectMake()的最后一个参数是rect的高度。您需要第二个参数。

您需要正确设置帧

int count=20;

for(int i = 0; i < [items count]; i++){
    UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,count,0,0)];
    label.text = @"text"; //etc...
    [label sizeToFit]; // resize the width and height to fit the text
    count+=20;

    [_scroll addSubview:label];
}
int计数=20;
对于(int i=0;i<[项目计数];i++){
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0,count,0,0)];
label.text=@“text”;//等。。。
[label sizeToFit];//调整宽度和高度以适合文本
计数+=20;
[_scrolladdsubview:label];
}

正如rmaddy建议的那样……添加新行以调整标签的高度,假设您有一个包含字符串的NSMutableArray对象“items”

float previousLabelHeight = 0.0;
for(int i = 0; i < [items count]; i++){
       CGSize theSize = [[items objectAtIndex: i] sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(320, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap]; //can adjust width from 320 to whatever you want and system font as well
       float newLabelHeight = previousLabelHeight + theSize.height;
       UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,newLabelHeight,0,0)];
       label.text = [items objectAtIndex: i];
       [label sizeToFit]; // resize the width and height to fit the text
       previousLabelHeight = newLabelHeight + 5 //adding 5 for padding

       [_scroll addSubview:label];
}
float-previousLabelHeight=0.0;
对于(int i=0;i<[项目计数];i++){
CGSize theSize=[[items objectAtIndex:i]sizeWithFont:[UIFont systemFontOfSize:17.0]constrainedToSize:CGSizeMake(320,FLT_MAX)lineBreakMode:UILineBreakModeWordWrap];//可以将宽度从320调整为您想要的任何值,也可以调整系统字体
float newLabelHeight=先前的LabelHeight+size.height;
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0,newLabelHeight,0,0)];
label.text=[items objectAtIndex:i];
[label sizeToFit];//调整宽度和高度以适合文本
previousLabelHeight=newLabelHeight+5//为填充添加5
[_scrolladdsubview:label];
}
干杯


快乐编码。

这是一个可以从数组中动态添加标签的Swift版本

    var previousLabelHeight: CGFloat = 0.0;
    for dict in items {
        let text: String = "Some text to display in the UILabel"
        let size = heightNeededForText(text as NSString, withFont: UIFont.systemFontOfSize(15.0), width: scrollView.frame.size.width - 20, lineBreakMode: NSLineBreakMode.ByWordWrapping)
        let newLabelHeight = previousLabelHeight + size;
        let label =  UILabel(frame: CGRectMake(0, newLabelHeight, 0, 0))
        label.text = text
        label.sizeToFit() // resize the width and height to fit the text
        previousLabelHeight = newLabelHeight + 5 //adding 5 for padding
        scroll.addSubview(label)
    }
ios 7.0不推荐使用sizeWithFont:ConstrainedToSize, 我们必须使用NSString中的boundingRectWithSize方法

func heightNeededForText(text: NSString, withFont font: UIFont, width: CGFloat, lineBreakMode:NSLineBreakMode) -> CGFloat {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = lineBreakMode
    let size: CGSize = text.boundingRectWithSize(CGSizeMake(width, CGFloat.max), options: [.UsesLineFragmentOrigin, .UsesFontLeading], attributes: [ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle], context: nil).size//text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MA

    return ceil(size.height);
}

你的问题是你给标签的框架。宽度为零,因此您不会看到它。(也可以将它们放在完全相同的位置)或者,不使用sizeToFit,只需使用
CGMakeRect(0,count,desiredWidth,20)