iphone制作UILabel时出现问题
我遇到了一个愚蠢的问题。
我使用下面的代码通过代码创建/修改 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先在代码示例中分配 tabLabel1,然后针对不同的命名对象 tab1Label 运行一系列属性更新。
抱歉,如果我误解了这个问题,但是您为什么要创建第二个标签?根据您的描述的这一部分:
只需更新 .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:
Just update the .text property and any sizing needed why use a whole separate object?