动态UILabel - UIImage - UILabel订购设置

时间:2013-02-02 00:41:52

标签: objective-c uiview uiimage uilabel

我有一个tableview单元格需要看起来像这样:

enter image description here

基本上我在左侧有一个标签,它总是保持相同的长度。那没问题。在右侧,我有一个标签,其长度会发生变化,例如它会包含某个人的名字,这个名字在人与人之间会有所不同。该标签需要在单元格上正确对齐,也没问题。

问题在于,在动态标签的左侧,我想要一个静态UIImage,即动态标签的10个像素,但会随之移动。

思想?

这是当前的代码,图像是标签的子视图,但显然没有动态移动。

            // Add text label
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 0.0, 80.0, 45.0)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.text = @"Account";
        textLabel.opaque = NO;
        textLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6];
        textLabel.font = [UIFont boldSystemFontOfSize:15];
        textLabel.shadowColor = [UIColor whiteColor];
        textLabel.shadowOffset = CGSizeMake(0.0, 1.0);

        //Add the image
        UIImage *image = [UIImage imageNamed:@"image.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 13.5, 18, 18)];
        [imageView image];

        // Add the Next Value label
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 0.0, 100, 45.0)];
        valueLabel.textAlignment = UITextAlignmentRight;
        valueLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6];
        valueLabel.backgroundColor = [UIColor clearColor];
        valueLabel.opaque = NO;
        valueLabel.font = [UIFont systemFontOfSize:15];
        valueLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"name"];

        [cell addSubview:textLabel];
        [cell addSubview:valueLabel];
        [valueLabel imageView];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

2 个答案:

答案 0 :(得分:1)

试试这段代码:

    // Add text label
    // same as before
    // ...

    NSInteger bufferFromEndOfCell = 10; // how much space you want between the end of your valueLabel and the end of the tableCell
    NSInteger widthOfValueLabel = 100; // dynamic, change this to whatever width you want
    NSInteger startingXlocationForValueLabel = tableView.frame.size.width - bufferFromEndOfCell - widthOfValueLabel;

    // Add the Next Value label
    UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(startingXlocationForValueLabel, 0.0, widthOfValueLabel, 45.0)];
    valueLabel.textAlignment = UITextAlignmentRight;
    valueLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6];
    valueLabel.backgroundColor = [UIColor clearColor];
    valueLabel.opaque = NO;
    valueLabel.font = [UIFont systemFontOfSize:15];
    valueLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"name"];

    //Add the image
    NSInteger widthOfImage = 18;
    NSInteger bufferBetweenImageAndLabel = 8; // the gap between your UIImageView and your valueLabel
    CGRect rectForImageView = CGRectMake(valueLabel.frame.origin.x - widthOfImage - bufferBetweenImageAndLabel, 13.5, widthOfImage, 18);
    UIImage *image = [UIImage imageNamed:@"image.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectForImageView];
    imageView.image = image;

    [cell addSubview:textLabel];
    [cell addSubview:valueLabel];
    [cell addSubview:imageView];

我确信有更清洁的解决方案,但我只是快速解决了这个问题。这会强制图像视图使用正确的标签动态更改。如果这不是你想要的,请告诉我。我只是基于我的信息。

答案 1 :(得分:0)

如果您的目标是iOS 6,我建议使用'AutoLayout'。

其他方面,其他答案提供的代码可以正常工作。

还有很棒的工作。

制作'UIButton',将样式设置为自定义并设置图像和名称。

希望它适用于你。;)