Mayur P Bhansali
Mayur P Bhansali

Reputation: 25

How do I add Two UILabels in my UIScrollView for the iphone?

myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];

lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];

lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];

But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?

Upvotes: 1

Views: 2857

Answers (5)

PJR
PJR

Reputation: 13180

lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];

Upvotes: 0

sarunw
sarunw

Reputation: 8176

After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];

Upvotes: 2

Zhang
Zhang

Reputation: 11607

Hello Mayur P Bhansali,

You can find the position and dimension of your first label like so:

lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];

UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];

Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.

You could access these properties like this too:

[labelName frame].size.width
[labelName frame].size.origin.x

Upvotes: 0

Solid Soft
Solid Soft

Reputation: 1932

This is your scroll view code.

myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];

After you set two label With CGRect and add in Scroll View

[myScrollView addSubView:lblluckyno];

......

Upvotes: 0

Baby Groot
Baby Groot

Reputation: 4279

[myscrollview addSUbview:lblluckyno];

and same way add 2nd label.

Upvotes: 0

Related Questions