Adding a UILabel on a UIImageView inside a for loop - ios

All the similar questions on SO and on the net are regarding placing a UILabel on a UIImageView, which is something i am familiar with, however i have to place multiple UILabel's on multiple UIImageView's. One on each UIImageView. I have a for loop like this:
for(int i=0;i < [arrURL count] ;i++)
{
UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 400, 180)];
y = y + 182;
view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arrURL[i]]]];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(view_Image.frame.origin.x, view_Image.frame.origin.y, view_Image.frame.size.width, view_Image.frame.size.height)];
[lblName setText:arrNames[i]];
[view_Image addSubview:lblName];
[mainScroll addSubview:view_Image];
}
When i run the code, the UILabel appears on only the first UIImageView, for the rest the UIImageView appears on the top of the UILabel. (I found this by adding background color to the UILabel). How can i make the UILabel's appear on the UIImageView. Thanks in advance.

A view's frame is in it's superview's coordinate system. So each image view's frame is in mainScroll's coordinate system.
Since each of your labels is a subview of a separate image view, each label's frame is in the coordinate system of its parent image view. But you are acting as though the label's frame is in the coordinates of mainScroll.
UILabel *lblName = [[UILabel alloc] initWithFrame:view_Image.bounds];

Related

how to make a rectangle at run time to fit a dragged label of words in it objective c

I am making a words match app in iOS in which i have two parts of words strings and I want to make a complete word from two labels of random words so for all is fine.
Now I want to make a rectangle at run time as a target of draggable label, when I click on label to drag it the rectangle should become hi-lighted with the same size as the size of label of words.
How can i achieve this in objective-C?
For more clearness you can see in image where I want to make this green rectangle at run time to drop the right side labels in it
The left side labels are not moveable and should always in the rectangle as you see in given image.The code so far i try to make the rectangle as a UIView in viewDidLoad as
for(int i = 0; i <5;i++){
customView = [[UIView alloc]initWithFrame:CGRectMake(10,y,50, 30)];
customView.backgroundColor = [UIColor greenColor];
[gameLayer addSubview:customView];
y = y+40;
}
But this is not what i actually want. Any help appreciated...
Finally i got the rectangle size same as the size of tapped label as here, i make an array in .h file which contain the reference of my target rectangle NSMutableArray *rectangleLabels
after that i make the size of rectangle same as the size of my draggable labels using a loop in tapped gestures method and it works fine...
-(void)gotTapped:(Id)sender {
for (UIView *v in rectangleLabels) {
v.hidden = !v.hidden;
UILabel *tapLbl = (UILabel *)[sender view];
CGRect rect = tapLbl.frame;
for(int i=0;i<rectangleLabels.count;i++) {
UILabel *lblChange = (UILabel *)[rectangleLabels objectAtIndex:i];
lblChange.frame = CGRectMake(lblChange.frame.origin.x, lblChange.frame.origin.y, rect.size.width, rect.size.height);
}
}
}

Dynamically Add and Center UIImages in UIView

I am trying to think through this fun "problem".
I have a UIView (288x36) that will hold up to 8 UIImages (36x36).
Any number (up to 8) can be placed in the UIView.
I want the "collection" of these images to be centered in the UIView.
If there is only 1 image, then it's center will be in the center of the UIView. If 2, then the max width of the image will be in the center of the UIView and the 2nd images starting point will also be in the center, etc etc.
Has anyone dealt with this before? Any code examples.
I am not very advanced but trying to learn.
Suppose your UIView is called view and your array of images is imageArray:
float remainingSpace = view.bounds.size.width - imageArray.count*36; //This is the space to remaining
float spaceOnLeft = remainingSpace/2.0; //Just the space to the left of the images
for (UIImage *image in images) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(spaceOnLeft, 0, 36, 36)];
imageView.image = image;
[view addSubview:imageView];
spaceOnLeft += 36.0; // so the next image is spaced 36 points to the right
}

addSubview causing another UIImageView to refresh

I have a UIView in which I placed a UIImageView which the user would be dragging around. Other UIImageView objects are created periodically using NSTimer and move around the view automatically using another NStimer. Each time a new UIImageView is added using addSubView, the user controlled UIImageView flickers back and forth it's original position. How do I deal with this?
Below is the code for the dynamically generated UIImageView. Circle is a subclass of UIImageView.
int x = arc4random() % 300;
Circle *tmpImg = [[Circle alloc] initWithImage:[UIImage imageNamed:#"blackCircle.png"]];
[tmpImg setFrame:CGRectMake(x , 0, 8, 8)];
[self.view addSubview:tmpImg];
[self.view bringSubviewToFront:tmpImg];

How do I make a UIImageView not be wider than the view it's contained in?

I set the image of a UIImageView to an image that is 1024x1024, and as a result a lot of the image is not visible, especially width wise, and cut off the edges of the screen.
I tried using clipsToBounds:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.clipsToBounds = YES;
[darkOverlayView addSubview:imageFromLink];
But it doesn't seem to do anything, and the image is still too big for the view.
You're going to want to change the contentMode property for the desired effect. UIViewContentModeScaleAspectFill is most likely what you'll want.
You have the set the image view's frame to the size you want. By default it will be the size of the image.
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = CGRectMake(0, 0, desiredWidth, desiredHeight);
[darkOverlayView addSubview:imageFromLink];
Also set the contentMode so the image is scaled properly in the smaller image view.
You should set the imageview's frame equal to the container's bounds.
UIImageView *imageFromLink = [[UIImageView alloc]initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = darkOverlayView.bounds;
[darkOverlayView addSubview:imageFromLink];

uitableview.backgroundView image dimension is compressing/distorting

I'm building an iphone app.
I have a tableview and i set a background image like so:
UIImageView * bg = [[UIImageView alloc] initWithImage:somebackgroundimage];
CGRect framebg = bg.frame;
framebg.origin.y = -64;
framebg.origin.x = 0;
framebg.size.width = 320;
framebg.size.height = 480;
[bg setFrame:framebg];
[_tblview setBackgroundView:bg];
no matter what framebg.size.width or framebg.size.height I set, the image always appears distorted or compressed with a size of maybe 320 width and 400 height. It is almost as if the app will forcefully resize the image to fit between the top navigation bar and my bottom tab bar.
How do I force tableview background image to be of 320px width by 480px height?
Also it seems the origin.y and origin.x aren't being respected either.
I know this question is a bit old, but I thought i would share what I was able to use to get around a similar issue (using setContentMode for the image view):
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[bg setContentMode:UIViewContentModeScaleAspectFit];
self.tableView.backgroundView = bg;
Hope this helps.

Resources