iphone制作UILabel时出现问题

发布于 2024-09-10 04:39:32 字数 1128 浏览 3 评论 0原文

我遇到了一个愚蠢的问题。

我使用下面的代码通过代码创建/修改 UILabel。我通过代码创建它的原因是因为我需要将它旋转 90 度,而我不知道在 IB 中执行此操作的方法。

发生了什么 - 用户点击一个按钮,使他们选择的文本出现在 UILabel 中。然后,当他们再次选择具有不同文本的按钮时,新文本将出现在旧文本的位置。

我第一次点击按钮时,它工作得很好,但第二次点击按钮时,新标签出现在旧标签上,并且旧标签永远不会消失。我尝试删除第一个标签,使其为零,仅删除文本,但一旦创建标签,我就无法访问该标签的任何部分。

ViewController.h

... UIView *viewForLabels; UILabel *tab1Label; } @property(非原子,保留)IBOutlet UIView *viewForLabels; @property(非原子,保留)IBOutlet UILabel *tab1Label …………

@end

ViewController.m

@synthesize tab1Label;

UILabel *tab1Label = [[UILabel alloc]init];

tab1Label.text = [theText];
tab1Label.backgroundColor = [UIColor clearColor];
tab1Label.textColor = [UIColor blackColor];
tab1Label.opaque = NO;
tab1Label.font = [UIFont systemFontOfSize:14];
tab1Label.numberOfLines = 2;
tab1Label.adjustsFontSizeToFitWidth=YES;
tab1Label.transform = CGAffineTransformMakeRotation (90*3.1459565) / 180);
tab1Label.frame = CGRectMake(2,87,45,119);
[viewForLabels: addSubview: tab1Label];

I'm having kind of a dumb problem.

I am using the below code to create/modify a UILabel via code. The reason I am creating it via code is because I need it to be rotated 90 degrees and Im not aware of a way to do that in IB.

What's happening - A user hits a button that makes the text they selected to appear in the UILabel. Then when they select the button again, with different text, the new text appears in place of the old text.

The first time I hit the button, it works perfectly, but the second time I hit the button, the new label appears over the old label and the old label never disappears. I have tried removing the first label, making it nil, just removing the text, but I cannot access any part of the label once it has been created.

ViewController.h

...
UIView *viewForLabels;
UILabel *tab1Label;
}
@property (nonatomic, retain) IBOutlet UIView *viewForLabels;
@property (nonatomic, retain) IBOutlet UILabel *tab1Label
...

@end

ViewController.m

...

@synthesize tab1Label;

...

UILabel *tab1Label = [[UILabel alloc]init];

tab1Label.text = [theText];
tab1Label.backgroundColor = [UIColor clearColor];
tab1Label.textColor = [UIColor blackColor];
tab1Label.opaque = NO;
tab1Label.font = [UIFont systemFontOfSize:14];
tab1Label.numberOfLines = 2;
tab1Label.adjustsFontSizeToFitWidth=YES;
tab1Label.transform = CGAffineTransformMakeRotation (90*3.1459565) / 180);
tab1Label.frame = CGRectMake(2,87,45,119);
[viewForLabels: addSubview: tab1Label];

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-09-17 04:39:32

首先在代码示例中分配 tabLabel1,然后针对不同的命名对象 tab1Label 运行一系列属性更新。

抱歉,如果我误解了这个问题,但是您为什么要创建第二个标签?根据您的描述的这一部分:

发生了什么 - 用户点击了
使文本成为他们的按钮
选择出现在 UILabel 中。
然后当他们选择按钮时
再次,用不同的文字,新的
文本出现在旧文本的位置。

只需更新 .text 属性和所需的任何大小调整为什么要使用整个单独的对象?

First in your code example you alloc tabLabel1 and then run a bunch of property updates against a different named object tab1Label.

Sorry if I am misunderstanding the question but why are you creating a second label? Per this part of your description:

What's happening - A user hits a
button that makes the text they
selected to appear in the UILabel.
Then when they select the button
again, with different text, the new
text appears in place of the old text.

Just update the .text property and any sizing needed why use a whole separate object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文