Change width of UIImageView - iphone

How would you change just the width of a UIImageView in objective-c? Currently I'm doing this:
imageview.frame = GCRectMake(x,y,width,height);
But when I change the width it seems to just change the position rather then change the size.

try this:
CGRect frame = [imageView frame];
frame.size.width = newWidth;
[imageView setFrame:frame];

Related

iOS: Adding UILabel to zoomed UIScrollview was blurred

I am facing one strange issue when adding UILabel to zoomed UIScrollview problem.
I am adding two views to zoomed UIScrollview
1. UIImageView
2. UILabelView
Here the code am using to add UIImageView on UIScrollview
float recentZoomScaleValue = 4.5;
CGRect rect = CGRectMake(x, y, 150, 150);
UIImageView *signatureImage = [[UIImageView alloc]initWithFrame:rect];
[signatureImage setImage:image];
[signatureImage setFrame:rect];
//resize the frame to avoid the auto zoom
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[signatureImage setFrame:frame];
[self addSubView:signatureImage];
[self.scrollview addSubView:signatureImage];
Here the code am using to add UILabel on UIScrollview
float recentZoomScaleValue = 4.5;
txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, self.fontSize)];
[txtLabel setText:#"loganathan is a good boy"];
[txtLabel setFont:[UIFont fontWithName:txtLabel.font.fontName size:self.fontSize]];
[txtLabel setTextColor:[UIColor redColor]];
[txtLabel setBackgroundColor:[UIColor clearColor]];
[txtLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[txtLabel setFrame:frame];
[self addSubview:txtLabel];
But the problem is when ever i tried to add UILabel view it added with auto zoomed. Since the label text was blurred. I do not know why and what is the problem. Shall i use CATextLayer instead of UILabel?. Any help that might be appreciated.
Thanks
you have to set the contentsScale to the right value when ever the zoomScale changes. Or initial to the current zoomScale:
Example for dynamic zooming:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[yourLabel.layer setContentsScale:scale*[[UIScreen mainScreen] scale]];
[yourLabel.layer setNeedsDisplay];
}

UITextView will not resize

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.
Try calling the -[UIView sizeToFit] method.

adjusting the width of UIImageView proportionally when changing height

I have the following code:
UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
NSLog(#"IMAGE SIZE WIDTH IS %f AND HEIGHT IS %f", imgView.frame.size.width, imgView.frame.size.height);
[imgView setUserInteractionEnabled:YES];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
CGRect frame = imgView.frame;
frame.size.width = SCREEN_WIDTH_PORTRAIT;
[imgView setFrame: frame];
however, the height of this view did not change accordingly, what is wrong?
You're telling the view's content to scale, not the view itself. You will have to set both the width and the height to do that.
If you're just trying to scale with orientation changes, though, you could use the UIViewAutoresizingMask and set it like this:
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleHeight;
That will make it stretch when switching orientations automatically.
Edit:
Here's how you would change the height and maintain the ratio, and animate to the new frame:
CGRect frame = imgView.frame;
float ratio = frame.size.width/SCREEN_WIDTH_PORTRAIT;
frame.size.width = SCREEN_WIDTH_PORTRAIT;
frame.size.height *= ratio;
[UIView animateWithDuration:.25 animations:^{
[imgView setFrame: frame];
}];
The image will fit to the frame with the same height as you are only changing the width. So, depending on the aspect ratio of the image, the image may not change it's size. You could determine the aspect ratio of the UIImage and then calculate the height from that with the new width.

How to modify the position of UIImageViews subviews inside a UIScrollView?

I'm trying to create gaps between the UIImageViews, which are subviews added into a UIScrollView.
And I thought I could do the following by modifying the CGRect of the UIImageView.
CGRect frame = imageView.frame;
frame.origin.x = <some values>
frame.origin.y = 0
imageView.frame = frame;
By modifying the x values, I assumed it will help me with the position of the UIImageViews inside the UIScrollView.
However, I realized whichever values I set for the frame.origin.x does not really matter. As each UIImageView inside the UIScrollView will be positioned side by side without any empty space.
I think you're doing it in the wrong way. You can't edit view's position by
view.frame.origin.x = something;
You should assign a new frame, like this:
view.frame = CGRectMake(x, y, w, h);
In your case it will look like this:
CGRect frame = imageView.frame;
CGFloat frameX = frame.origin.x;
CGFloat frameY = frame.origin.y;
CGFloat frameW = frame.size.width;
CGFloat frameH = frame.size.height;
frameX = <some values>
frameY = 0;
imageView.frame = CGRectMake(frameX, frameY, frameW, frameH);

How to resize a UIImageView to fit the underlying image without moving it?

I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.
How can I resize the frame so it's the same size as the underlying image, while ensuring that the center point of the UIImageView does not change?
If you change the bounds of a UIView the center will not change.
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = newImage.size;
imageView.bounds = bounds;
imageView.image = newImage;
try something along the following lines.. not tested
CGSize imageSize = image.Size;
CGPoint oldCenter = imageView.center;
// now do some calculations to calculate the new frame size
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imageView.frame = rect;
imageView.center = oldCenter;