How to resize UIlabel inside UIView proportionally? - iphone

I have a UILabel inside a UIView and I want the label to be resized proportionally as the UIView is resize.
I was able to do so with a UIImageView but the label stays as big as I placed it in IB.
I have set the view contentMode to UIViewContentModeScaleAspectFit thinking that anything inside the UIView would be "aspect fit" but I had to set the same mode to the UIImageView (which works fine) and I can't set that mode to the UILabel.
Thanks for your help.

It appears CGAffineTransform is the answer.
myView.transform = CGAffineTransformMakeScale(0.5, 0.5);
This would resize the view and everything that is inside dividing the view width and height by 2.

set setAutoresizingMask of label

Related

iOS Swift : Keep UILabel constantly centered in UIView that changes height

I have a UILabel centered within a UIView that expands/contracts depending on the device screen size. I've applied constraints so that the UILabel remains centered no matter the UIView size, which works fine.
Now I'm finding myself resizing the UIView manually like so (where mainView is the View Controller):
self.myView.frame = CGRectMake(0,0,self.mainView.frame.width, self.mainView.frame.height)
So this stretches the UIView to fill the whole screen/View Controller (it's also animated). I assumed the UILabel would continue to centre itself automatically, but it seems to pin itself as though it were constrained to the top of the UIView, leaving a lot of empty myView space below it.
How can I tell the UILabel to remain in the centre of the height-changing UIView that it's in?
We need to see your constraints to help, but:
You need to use centering constraints.
Set the bounds, not the frame of myView.
You might need to call setNeedsLayout on the view of the ViewController you are in
Also, set the background color of the UILabel -- it could be centered, but the text is not centered inside of it. For that, set the alignment properties.

Crop UIImage to fit a frame image

I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded borders and so on). Check the image below:
Using Aspect Fill
Using Aspect Fit
So, what I need is to remove the image excess that is out of the frame bounds.
I tried using UIBezierPath, CGImageRef and other methods that I Googled but I'm not finding a solution for this.
In Interface Builder, use the following configuration:
There are two important settings, namely:
Mode: Aspect Fill
Clip Subviews
It can also be done programmatically:
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];
This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.
make a IBOutlet in your controller.
#property (retain)IBOutlet UIImageView* imageView;
and in -(void) viewDidLoad set
imageView.layer.masksToBounds = YES;
In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").
OLD ANSWER:
You can use the contentGravity property of CALayer to make it work
A constant that specifies how the layer's contents are positioned or scaled within its bounds.
#property(copy) NSString *contentsGravity
Use an UIImageView and do
imageView.contentMode = UIViewContentModeScaleAspectFit;

Displaying a UIImageViews image to the size of the actual image

How does one set a UIImageview's image to the actual image size rather than filling the view with the image and thus stretching the image(if the image is smaller than the UIImageView's size)
You want to adjust the contentMode property. I suspect to UIViewContentModeCenter.
Create the UIImageView like this:
UIImageView *aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
So the ImageView has the size of the image.
I hope my answer helps you.
Note that if you're in IB or a Storyboard, click on the Attributes inspector tab after selecting your imageview, look down in the View section (under ImageView) and where it says "mode", change the selection from "scale to fill" (the default) to "center"
I think this is basically the IB equivalent of Tommy's answer, which does the same only programmatically.

iPhone autoresizing

Is there a "stick-to-bottom" autoresizing mode in Cocoa-Touch? Basically, I got a UIImageView in the lower part of another UIView. When the UIView resizes, I don't want to change the UIImageView's size, but keep it in the lower part of the UIView, while only resizing the other subviews in my UIView above the UIImageView.
Is that easily feasible?
Assuming you want to have the image view resize horizontally:
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

How to scroll the content without scrolling the image in the background using UIScrollView?

I am trying to scroll the content without scrolling the image in the background using UIScrollView.
I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.
I have also set scrolling content size in the vierController that owns this UIView
(void)viewDidLoad {
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 960);
}
My problem is that the Image only appears moves along with the content.
I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.
I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).
Any advice would be appreciated.
The easiest (and correct way) is to set an background image to your UIScrollView
UIImage *img = [UIImage imageNamed:#"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.
Thanks. This was extremely helpful.
The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).
I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:
// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);
// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:#"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".
I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.
Jason