I have a number of UILabels on a view and a varying amount of text in each. I need the font size to be the same in each.
I’m using autolayout to position / size the labels.
However some of the “g” characters are being cut off.
I’m not sure how I can resolve this ?
change the lines property of the lables to 0 and hook then properly to each other or to the superView so when there content is large they wrap smoothly
You cannot control font size directly via auto-layout constraints. You can disable adjusting font on all labels and this way they will have same label.
But to better help answering your question (and underlaying issue you're trying to solve) - it would be much better if you share screenshot with your interface.
SizeToFit fixes my issue, rather than autolayout
Related
I have two labels (labelOne and labelTwo) which I want to have the same text size. Using autolayout labelTwo tends to be bigger than labelOne and as such has a larger font size. I have tried using the following code in viewDidLoad:
labelOne.font = UIFont(name: labelTwo.font!.fontName, size: labelTwo.font!.pointSize)
as suggested in this question: Autoshrink labels, but maintain same font size for every other UITableViewCell
but this does not work for me.
not sure if you've found a solution but I had a similar issue.
I solved it by making the labels the same width and aspect ratio, autoshrink on with minimum font scale. Here's the charm though: Lines=0.
Somebody mentioned having the same number of characters, but that doesn't work on all fonts.
Make sure you set the label to a fixed font size. Your font size is probably changing when the view controller calls layout subviews.
Labels have intrinsic content size. You need to make them have the width and height constraints and make an equality relation between them.
I’m building an iOS app which includes a quiz. Questions are displayed in the upper portion of the screen (see below). There are always five possible answers. The thing is: the answers are procedurally generated and vary in length, which leads to line breaks in the label sometimes.
This is the current state
Maybe it’s hard to tell from the picture, but the spacing between the first and second and then between the second and third is not the same as I intend it to be.
This is what I had in mind
Essentially, I’d like the top- and bottommost labels to have the same space towards the container. The labels in the middle should all have the same spacing between each other, but should also adapt, if one of the labels gets bigger (when the size of a text is longer than the width and a line break occurs).
To achieve this, I tried the following:
Organise the labels as a stack view:
Nearly worked, the only problem I have here is, that the size of the
labels is calculated after the stack view is displayed, which leads to wrong constraints/paddings/margins being applied to the (possibly) longer texts at runtime.
Organise the labels with regular constraints
I tried setting the
priority of the constraints for the middle labels lower than
that of the top- and bottommost ones, so those would be the ones
to resize, if the labels enlarges, but it appears that at
runtime one of those is chosen to shrink, whereas the others
remain at their default size.
I'd really appreciate if you could help me out, constraints always seem to be a pain in my neck...
Agreed... I think UIStackView works fine, just have to set it up properly.
Also you may need to call .sizeToFit on the labels when you set the text.
I put up an example for you - the sizing is not exact, but you should be able to follow the technique...
https://github.com/DonMag/StackViewFun
I think the best practice in your case would indeed be to use a UIStackView. Then you have multiple option in solving this problem:
You can set the distribution of the UIStackView to Fill Proportionally and set a Minimum Font Size or Minimum Font Scale to your UILabels.
You can also set the distribution of the UIStackView to Fill and then manually specify the height of your containers. The UILabels should still have some Minimum Font Size or Minimum Font Scale.
EDIT:
I just realized you want to keep all UILabels to have the same font sizes. Then a possible solution would be to embed the UIStackView into a UIScrollView and constraint the UIStackView's Leading, Trailing, Top and Bottom constraints to the ones of the ContentView of the UIScrollView. In this way the height of the UIScrollView will adapt in accordance of the needed height by the UIStackView.
Hope this will help you!
I have some ASCII syntax diagrams which must not have line breaks in the middle.
These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle.
I just want to shrink only if the content doesn't fit the width. It would be absolutely fine to scroll vertically through the text.
What is the best way to do this with UILabel or any other UI element?
Use UITextView with 'editable' property set to false.
So let me rephrase your question. I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. If this is what you want, well the imagination is weird to me...
But anyway, I feel there's a conflict in your settings. First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. But then Autoshrink will play no effect. I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code.
I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. I guess you want to take a look about this:
Autoshrink on a UILabel with multiple lines
The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. Maybe this can achieve what you want.
I'm pretty new to the whole autolayout design and I'm trying to figure out the best way to make this design compatible on all sizes. I can't seem to find a way to space out each element so the size of each element changes according to the screen size. I know that I can constrain the subviews to the margin but Xcode seems to require a set width and height so the larger screen version ends up looking extremely compressed. (see below) What would be the best way to handle a custom form like this? Basically I need a way to dynamically space out each element.
Each element is on its own. IE text,dividers,icons
If I understood your issue correctly then I think!you need to set leading,top,tailing and bottom space and aspect ratio if necessary for change in height of textfield to the view of which you need to increase or decrease size according to its width.
I am working with the new constraints with iOS6. I have two UITextViews with a vertical spacing between them. Both UITextView's are dynamically populated the so the HEIGHT can be altered depending on the amount of content being populated.
What is happening is when to much data is loaded in the First UITextView it goes over the top of the second UITextView which is directly below it. I do have a constraint which is set between the two but it seems to ignore it. I have no code to show as it's on the storyboard.
Is there anyway to confine the vertical space between the to views no matter how much content is populated? Or has anyone come across this type of behaviour before?
Thanks in advance!
Jeremy