TazmanNZL
TazmanNZL

Reputation: 392

Get UILabel from UIView

I have added a number of labels to a view giving them tags.

What is the correct way to retrieve a label from the view. I am wanting to re-postion the label. Here is what I am using to retrieve the label and re-position it:

UILabel *theLabel = (UILabel *)[self.view viewWithTag:5];
[theLabel.layer setPosition:CGPointMake(100, 200)];

Is this the correct way of doing it?

Upvotes: 0

Views: 1461

Answers (3)

ashokdy
ashokdy

Reputation: 1001

You can use fast enumeration with a for-loop:

for (UIView *view in [self.view subViews]) {
     if([view isKindOfClass:[UILabel class]]) {
          // do your stuff here
     }
}

Try this, it will surely work.

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

To retrieve the label, use an IBOutlet if you've created it in Interface Builder, or a raw property or ivar if created in code. There's no need to loop the subviews or use viewWithTag if you've got a direct reference (outlet, property, ivar) to it.

To move the label, directly set its frame or center property without needing to access its layer.

Upvotes: 0

morningstar
morningstar

Reputation: 9162

There's no need to go to the layer level, although it might work fine.

theLabel.center = CGPointMake(100,200);

I guess that does the same thing, without looking at the documentation to verify that a layer's default anchor point is its center.

Upvotes: 2

Related Questions