Pixel Perfect UILabel - ios

Here are two labels stacked directly on top of each other, with their backgrounds colored:
I always design my apps in Photoshop first. That dead space on the top and bottom of the labels makes it extremely difficult to position text precisely as in the design. How can I compensate for that top and bottom space when I want to place the label by top or bottom of the text?

Try calling -sizeToFit on the label in order to resize it.
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.

Either use AutoLayout to size your labels exactly at the content size, or use sizeToFit in code and position your labels accordingly.

Can you not create a background UIView with the background colour, and then add the label as a subview of the background view?

Related

UILabel scales only for certain text lengths

I have a custom UIView containing a UILabel and a UISwitch. The switch and the label are constrained to be next to each other with their center-y equal and their sides matching the sides of the parent (with a margin). The label is constrained to connect with the top and bottom of its parent (again, plus margin) with its number of lines set to 0 so it can scale as large as it needs to be to fit all the text, using word wrapping as the line break mode. I have a bunch of these custom views below each other in a vertical UIStackView with alignment and distribution set to fill. The stack view itself is positioned inside a scroll view which is constrained to all sides of the root view (which scrolls as expected). Here's a screenshot of the situation that also shows my problem:
I've added a red background to the label in the custom view to show its boundaries. Separator views are added between the custom views. The problem becomes apparent in the second custom view in this screenshot. Its label text is actually WordOne WordTwo WordThree WordFour, but for whatever reason the text is wrapped without the label / parent increasing their size to contain it. Adding an extra word, as in the view below it, suddenly does scale the label to show all of its lines. I've set compression resistance to UILayoutPriorityRequired for both the label and the parent, to no avail. I have a workaround involving a layoutSubviews override with a manual calculation of the label size, but I'd prefer both understanding what's going on and of course avoiding having to use a hack. Can anybody shed some light on this behavior?

Using AutoLayout to grow a view based on dynamic text in subview

I'm working on an app that supports dynamic type. I have a simple UIView that is used as the footer in a table. Within that view is just a UIButton. When the user increases the dynamic type size, the text in the button's label grows, and I need the button itself, and the containing view to both grow appropriately. How can I achieve this?
Thanks,
The UIButton class has an intrinsic size, meaning that if you don't constrain it to a specific size it will size itself to a size that will fit its content. This also means that you can tie your button to the edges of a parent UIView and let the button control the size of the parent. This will happen automatically as long as you don't explicitly constrain the size of the UIButton or UIView.
If you want to, you can choose to set a constraint on one axis and have the button control the size of the other, for example let the button control the height of your footer, but always use a fixed width for the footer. In this case you add constraints to the width or left/right of your footer.
Read more on controls that supports intrinsic sizes
If you need space around the button label you can add it by increasing the content insets.

Fix objects inside subview into place

How can I fix objects inside a view to a position? What I mean is: I have a subview which includes multiple elements for example an image and a textfield. Those subviews mustn't change their aspect ratio. I know I can set that via auto-layout. But how can I set that all the elements in the subview stick relative to their position inside of that subview? So that if I resize the subview (while remaining in their aspect ratio) the content of it will look the same just up- or downscaled?
Thank you!
UPDATE: This is how it looks like. The white area in the middle is the 'subview', and the textfield together with the labels the 'content':
Now if the screen size of the device changes it looks like that:
I have a constraint saying that the 'subview' should keep its aspect ratio. And I just want the 'content' to resize accordingly.
If I try to auto-layout the textfields and labels to 'subview' I get many weird errors. Isn't there any easier method? I mean it's just resizing and not some complex UI changes.
I haven't tried but, if you are scaling your view then inner views are will scale in ratio of the outer view.
But if your plan is to stretch outer view from the corners may be this layout setting can be help full.
Inside the storyboard you have an option to give something the same height or width as the parentView if you ctrl + pull to it.
Then it will say x equal to parent y = 1 whewre 1 is 100% of size, size it down to for example 95% and the align the views in the center, left or right or wherever you would like.

How do I make two equally sized imageviews cover the whole screen

I implemented a viewcontroller similar to the one in this question:
iOS two views cover exactly half of parent view
I get the desired result. The problem is that when I add a UIImageView into those two containers, the result get resized and ruins the symmetry. How do I prevent Imageviews from changing the size of their containers? I need to use AspectFill for these images.
I don't think the containers' sizes are changed. You just need to set their clipsToBounds property to true to avoid overflow.
If you use Reveal you should see the containers' sizes are not changed even if pictures inside them are bigger than themselves.
I'm unsure I understand what you mean about adding UIImageView into those controllers causing the views to resize, so forgive me if I'm getting this all wrong...
It sounds as though you've created two views of equal height that, together, consume the entire vertical space of the screen. After having done that, you want to add one or more UIImageView's to each of the original two views. Unfortunately, when you add the UIImageView, the enclosing view is resized.
Assuming I have that correct...
Are you doing this with Interface Builder either in an XIB or Storyboard file? If so, you ought to be able to achieve this with the proper set of constraints.
In the following image, I've laid out what I describe above.
As you can see, I have a red view on the top half of the window and a green view on the bottom half. The red view contains a UIImageView that is 75% of the width and height of red view, with its origin at (20, 20) within the red view.
The scene is configured as shown below:
The constraints on "Upper View" are:
You can see from this that Upper View is flush with the left, right, and top of its superview, and that its space to Bottom View is 0. You'll have to trust me that Bottom View is set up the same way.
The height of the Upper and Lower views is "Proportional" as shown in this constraint:
To achieve this "Proportional" setting, you first make the height of Upper View equal to the height of the superview, and then edit the constraint, changing "Multiplier" from "1" to "0.5."
The height (and width) of the Image View is proportional to that of the Upper view, as shown here:
If you set it up this way, you ought to be able to accomplish what (I think) you are looking to accomplish.
If my original assumption of what you are trying to achieve is incorrect, please post images of what you've got and how it's not working.

Best practical way to draw line over UIImageView?

I've been reading a lot of posts here and I see conflicting information.
I'm trying to draw a line over a UIImageView which already contains a picture.
I'll need to draw the line either horizontally, vertically, or not at all based on user selection, without disturbing the existing picture.
Some responses say to draw into the UIImageView directly, some say to subclass a UIImageView or UIView, and still others say to create a view layer.
What is the Best Practice for this, and can you also point me to a tutorial?
Since you need to pan/zoom the image underneath, I suggest you implement a stack like this:
[UIScrollView] on the bottom
[UIImageView] inside the scroll view for pan/zoom
[UIView] on top of the scrollview - for drawing the line
(see this tutorial for line drawing).
Personally I've used this project for implementing pan/zoom -- it's well implemented and takes a lot of the guess work out of it.
The easiest way to do this, since you only want to draw a horizontal or vertical line, is to add a subview to the UIImageView. If you want a horizontal line, set the subview's height to 1-2 points and set the width to the image view's width. If you want the line to be vertical, set the subview's height to the image view's height and set the width to 1-2 points. Set the subview's background color to whatever color you want the line to be.
A subview always appears on top of its superview.

Resources