如何改变UILabel的大小

时间:2011-05-21 08:43:45

标签: iphone objective-c

我有一个以编程方式编码的UILabel。我想按下按钮时更改标签的大小。如何更改该标签的大小?这是我的代码

 UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];  
[theLabel11 setText:@"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:@"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];    

3 个答案:

答案 0 :(得分:17)

您应该将标签声明为类属性,以便可以从其他方法访问

要更改字体大小,请使用

[theLabel11 setFont: [UIFont fontWithName:@"Arial" size:13.0f]];

要更改标签我们的框架尺寸

theLabel11.frame = CGRectMake(x, y, width, height);

答案 1 :(得分:11)

调整UIView空间信息的常用习惯用法如下

label.frame = CGRectMake(
    x,
    y,
    width,
    height
);

您可以通过

获取旧位置和高度
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height

答案 2 :(得分:0)

如果只有一个标签添加到scroll1,则迭代scrollview以获取标签参考,如下所示:

for(UIView *subView in scroll1.subViews){

if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}

如果有许多标签在创建时为每个标签分配标签并检查for for循环

相关问题