开发者

how to change the size of UILabel

I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the 开发者_如何学Gosize of that label? this is my code

 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];    


You should declare your label as class property, so it can be accessed from other methods

To change the font size use

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

To change the frame size of the label us

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


A common idiom for adjusting the spatial information on a UIView is as below

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

You can get the old position and height via

label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height


If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action

for(UIView *subView in scroll1.subViews){

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

if there are many labels assign a tag to each label while creating and check that in for loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜