Get UILabel from UIView - ios

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?

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.

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.

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.

Related

Xcode / iOS / iPhone How to set event to UILabel

Please give me an advice.
I create UILabels programmatically (dynamic).
Is there is a chance to add Event to them?
What I want by steps:
I create UILabel;
I set Event to it; (NSNotification?)
When I do some action (rotate, for example) I want that Label is changed or removed. An extended example: I create Labels and when I rotate device I want that part of them (which with attached Events) disappear in animation.
I create a lot of Labels, so I can't just set them global variables. And I can't set them tags unlimited. So UILabel *label = (UILabel*)[self.view viewWithTag:labelCount not a solution. Getting element by 'viewWithTag' has one more trouble - when set animation to that element and that element already in animation happens collision - they plays one over other...
I create Labels like this:
CGRect *labelFrame = CGRectMake(left, top, width, height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.text = #"Hi, I'm one of these army of labels";
[self.view addSubview:label];
PS: Sorry for English.
I assume you have a UIViewController that has a bunch of labels. I would recommend an IBOutletConnection for storing a reference to all of your labels (assuming storyboard).
//You will need to connect all of these labels through Interface Builder.
#property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
In one of the following rotation methods (Detect rotation changes in iOS) do your rearranging.
//Called whenever orientation changes
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (UILabel *label in self.labels) {
//Make each label disappear here.
}
}

How to add UIView into UIScrollView in iOS (View having dynamic content )

I want to create a scrollView having many sub views as shown in image.
All views having a labels & image within it.
Number of views add in scrollView are dynamic.
And data of that views is also dynamic.
So that I can't make a static view in program and use it for display.
I want to make scrollview's subview like TableView with custom cells.
Like make a object of that TableViewCell and use it.
Can I use ViewController for that?
If i understood you question true, you need something like dynamic content of scrollView. So you need an array to control how many cell you will put into scrollView and create label, imageView or whatever you need.
For example like that;
//You will need to clean your scrollView Content in everytime
for(UIView *view in [yourScrollView subviews]){
[view removeFromSuperview];
}
for(int i=0;i!=[yourArray count];i++)
{
labels[i]=[[UILabel alloc]init];
//anyInteger is about your views place.
views[i]=[[UIView alloc]initWithFrame:CGRectMake(21, i*anyInteger, 300, 50)];
views[i].backgroundColor=[UIColor colorWithRed:0.1 green:0.2 blue:1.8 alpha:0.0];
[views[i] addSubview:labels[i]];
[yourScrollView addSubview:views[i]];
}
These codes will help you about insert objects in yourScrollView. I didnt test this yet but i guess it will give you an idea.

Getting subviews of a UIView within a UIScrollview

I have a UIScrollView that has UIViews in it, and in those UIViews are UIImageViews. I am trying to access the UIImageViews.
For proof of concept, I have a scrollview with a UIView, which contains a UIImageView in it, both whose tags are set to 0.
Code:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIView *currentView = (UIView *)[scrollView viewWithTag:0];
UIImageView *currentImageView = (UIImageView *)[currentView viewWithTag:0];
[self bobbleView:currentImageView];
}
And then I am trying to bob that UIImageView up and down continuously (which is another matter in of itself, I can only get it to bobble once so I just that left that code in), but what is happening is the entire UIScrollView and its subviews are bobbing.
That code:
-(void)bobbleView:(UIView *)viewIn{
viewIn.transform = CGAffineTransformMakeScale(1.2, 1.2);
[UIView animateWithDuration:1 animations:^{
viewIn.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
}
Any thoughts?
The viewWithTag doc says:
Discussion
This method searches the _current view_ and all of its subviews for the specified view.
(emphasis mine)
What you are seeing is that:
(UIImageView *)[currentView viewWithTag:0]
returns
currentView
HTH
Yes.. Assign unique tags to the views and different to eachother. I bet that if you set a breakpoint in the call to bobbleView, your UIImageView object will be your UiScrollView and not the imageview nor the UiView... Use simply unique tags, like 100X fir the UIViews and 200X for the imageview

Getting all subviews for a UIScrollView

I need to get an array of all the subviews in a UIScrollView. Right now I'm using
NSArray *subviews = [myScrollView subviews];
but this seems to only be returning the subviews that are visible at the time the code is run. I need all the subviews in the whole extent of the UIScrollView, even those that are currently hidden (as in off screen). How would I get that?
Essentially, I'm looking for something like the contentSize property of a UIScrollView, except instead of returning just the size of the UIScrollView if it were big enough to display all of it's content, I want it to return the content itself.
EDIT: I think I've figured it out: the scroll view this isn't working for is actually a UITableView - and I think it's deque-ing the cells that are off screen on me, and that's why they aren't showing up. I'm going to do some testing to confirm.
Try with following code its working for me.
for(UIView * subView in myScrollView.subviews ) // here write Name of you ScrollView.
{
// Here You can Get all subViews of your myScrollView.
// But For Check subview is specific UIClass such like label, button, textFiled etc.. write following code (here checking for example UILabel class).
if([subView isKindOfClass:[UILabel class]]) // Check is SubView Class Is UILabel class?
{
// You can write code here for your UILabel;
}
}
tl;dr
It turns out that
NSArray *subviews = [myScrollView subviews];
will indeed return all the subviews in a UIScrollView *myScrollView, even if they are off-screen.
The Details
The problem I was actually having was that the scroll view I was trying to use this on was actually a UITableView, and when a UITableViewCell in a UITableView goes off-screen, it actually gets removed from the UITableView - so by the time I was calling subviews, the cells I was looking for were no longer in the scroll view.
My workaround was to build all of my UITableViewCells in a separate method called by my viewDidLoad, then put all of those cells into an array. Then, instead of using subviews, I just used that array. Of course, doing it this way hurts the performance a little (in cellForRowAtIndexPath you just return the cell from the array, which is slower than the dequeueReusableCellWithIdentifier method that is typically used), but it was the only way I could find to get the behavior I needed.

iOS: How to check if a label exists in a certain area?

In my application I include a lot of different labels (they represent street names). Therefore I do some calculation to show them in the right angle and on the right spot. Now I want to check wether there is an existing label with the same text in the area where I want to include the new one, but I'm not sure how to manage this. Can I check for an intersection maybe?
I'm looking forward for help
You can fetch UILabel from UIView in following manner -
for (id view in self.view.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *) view;
}
}

Resources