首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态UILabel - UIImage - UILabel有序设置

动态UILabel - UIImage - UILabel有序设置
EN

Stack Overflow用户
提问于 2013-02-02 08:41:52
回答 2查看 774关注 0票数 0

基本上,我在左边有一个标签,它将始终保持相同的长度。这没问题。在右边我有一个长度会改变的标签,例如,它将包含某人的名字,这显然是因人而异的。标签需要在单元格上右对齐,也没有问题。

问题是,在动态标签的左边,我想要一个静态的UIImage,它离动态标签有10个像素,但会随着它移动。

有什么想法?

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

代码语言:javascript
复制
            // 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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-02 11:40:21

尝试以下代码:

代码语言:javascript
复制
    // 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
EN

Stack Overflow用户

发布于 2013-02-02 12:27:11

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

否则,其他答案提供的代码可能会起作用。

还有一项很棒的工作。

创建一个'UIButton',设置样式为custom,并设置图像和名称。

希望它能为你工作。;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14657398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档