使用标签/UILabels/UIViews

Posted

技术标签:

【中文标题】使用标签/UILabels/UIViews【英文标题】:Working with Tags/UILabels/UIViews 【发布时间】:2013-07-17 01:22:38 【问题描述】:

目前,

我正在我的应用程序中实现一项功能,该功能可以在 UIView 的子视图中动态分配具有 2 个 UILabel 的 UIViews...我以前做过,但由于某种原因似乎被卡住了。

这是我的代码:

UIView *view;
UILabel *label;
UILabel *powerOutput;

for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)

    view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
    view.backgroundColor = [UIColor blackColor];

    label = [[UILabel alloc] init];
    label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    [powerOutput setTag:LABEL_TAG+i];
    NSLog(@"%@", label.text);
    [view addSubview:label];

    powerOutput = [[UILabel alloc] init];
    powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
    powerOutput.backgroundColor = [UIColor clearColor];
    powerOutput.font = [UIFont systemFontOfSize:10];
    powerOutput.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    [powerOutput setTag:IMAGE_TAG+i];
    NSLog(@"%@", powerOutput.text);
    [view addSubview:powerOutput];

    [self.view addSubview:view];




for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++) 

    UILabel *label = (UILabel *)[self.view viewWithTag:LABEL_TAG+i]; // get the label with tag
    //NSLog(@"%@", label);
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    //NSLog(@"%@", label.text);

    UILabel *powerOutputLabel = (UILabel *)[self.view viewWithTag:IMAGE_TAG+i]; // get the label with tag
    powerOutputLabel.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    //NSLog(@"%@", powerOutputLabel.text);

我也试过了:

UIView *view;
UILabel *label;
UILabel *powerOutput;

for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)

    view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
    view.backgroundColor = [UIColor blackColor];

    label = [[UILabel alloc] init];
    label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    //[powerOutput setTag:LABEL_TAG+i];
    NSLog(@"%@", label.text);
    [view addSubview:label];

    powerOutput = [[UILabel alloc] init];
    powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
    powerOutput.backgroundColor = [UIColor clearColor];
    powerOutput.font = [UIFont systemFontOfSize:10];
    powerOutput.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    //[powerOutput setTag:IMAGE_TAG+i];
    NSLog(@"%@", powerOutput.text);
    [view addSubview:powerOutput];

    [self.view addSubview:view];



这应该没有任何问题,因为当我创建一个新视图时,我会创建一个新的 UILabel,并将其添加到 NEW UIView...

这是一张照片:

所以我创建了三个视图,好吧……但是我的 3 个 UILabel 在哪里……还有一个文本的日志声明:

2013-07-16 18:14:26.945 PowerOneApp[14307:c07] SN 875222
2013-07-16 18:14:26.945 PowerOneApp[14307:c07] 53.50W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 875225
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] 59.89W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 957188
2013-07-16 18:14:26.947 PowerOneApp[14307:c07] 135.39W

真的不知道为什么这不能正常工作......有人有什么想法吗?在这一点上,这似乎是一件非常微不足道的事情,就像一行代码之类的......我在使用具有多个这样的视图的标签之前已经这样做了,并且对于不同的 UIViews 具有不同的值,并且 UILabels 值随着关于我实现的 UISlider。

所以我对这个问题有一些经验,但我现在很困惑。也许明天我会明白我做错了什么,但目前....我真的不确定...如果有人可以指点...请这样做:)

【问题讨论】:

【参考方案1】:

您的标签框架设置不正确:

label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);

Frame 已经相对于父视图,因此当您添加父视图的原点 xy 时,您的标签会超出可见范围。这适用于第一个视图,因为它的 xy 很小。

你可能想写:

label.frame = CGRectMake(10, 12, view.frame.size.width - 10, 20);

第二个标签也一样。

【讨论】:

同意,或者在父视图中添加布局约束更好。 嗯...好吧...明天我到办公室我会试一试...谢谢您的意见:)【参考方案2】:
label.textColor = [UIColor whiteColor];

***powerOutput.textAlignment = NSTextAlignmentCenter;***

label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];

**powerOutput.textAlignment = NSTextAlignmentCenter;**的语句首先运行时,你的变量**powerOutput****nil**

【讨论】:

以上是关于使用标签/UILabels/UIViews的主要内容,如果未能解决你的问题,请参考以下文章

JSTL标签库

git:使用标签

struts标签的使用

dedecms标签的使用方法有哪些?

学会怎样使用Jsp 内置标签jstl标签库及自定义标签

JSTL 标签库 使用