2

NSMutableArray *items // 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];

    }

どうしようかな ありがとう

4

4 に答える 4

5

フレームを適切に設定する必要があります。

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];
}
于 2013-09-10T00:12:42.800 に答える
0

フレームの Y 値を変更しようとしていると思いますが、CGRectMake() の最後のパラメーターは四角形の高さです。2 番目のパラメーターが必要です。

于 2013-09-10T00:05:33.577 に答える
0

これは、配列から動的にラベルを追加する 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)
    }

sizeWithFont: ConstraintedToSize は ios 7.0 から廃止されたため、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);
}
于 2016-05-21T07:34:04.530 に答える