UILabel autoshrink is not working using interface builder? - uilabel

I made a UILabel and set interface builder properties as
LINES "0"
LINE BREAK "WORD WRAP"
AUTOSHRINK "MINIMUN FONT SIZE"
But whenever a large text is set on label, it does not shrink. Why?
Is there any other thing is needed to be set ?

Auto shrink only works if you have constraints that fix the size of the label.
Either set a fixed "width" constraint on the label, or fix its width relative to its container, by pinning it to the superview.
edit: grammar is hard.

Related

Objective C multiline UILabel is not working

Here I have attached the scenario of my issue.
Current View
Here Top Title is a bit long string. But my view is shown partially.
Here I added the storyboard properties that I used for my UILabel.
Can someone suggest a solution to make this UiLabel as multilined one ?
You can't have a multi-line label with a fixed font size, if that label is constrained to a certain height.
Either the height needs to scale to allow more lines at that fixed font size, or the font size needs to scale down to fit all the text in.
If neither are possible, then the text will be truncated.
If your label is constrained to a certain height, then you can change the Autoshrink setting from Fixed font size to Minimum font size and specify the smallest size that you will allow the font to shrink to in order to accommodate longer strings.
Try to change the relation of constraint height: Greater than or equal

Changing button's size based on its content's length, so the button keeps its shape

I have a UIButton which is created programmatically (it can be customised from Interface Builder, though). It is a circular and it looks like this:
Now I'm working on Internationalization and I need to check it for Double-Length Pseudolanguage. The image above is taken when the app language is set to double-length pseudo, but as you can see, the button only shows one word. Here are the constraints of the button:
The title style of the button is plain, its text is centre-aligned, and the line break is set to "Word Wrap".
How to make the button keep its shape but to increase its size when needed (basically, to increase its width and height for the same amount, so it keeps being a circle), so for example, in this case to show two "Menu" words? If you know how to achieve this I would appreciate your help.
Try setting the content compression resistance on the button to required (1000) for horizontal and vertical. It will also depend on the stackViews above and below allowing for the size change.

How can I use Autolayout to create a layout that expands in height based on the content of a UILabel

I am attempting to build a dynamic layout using the new autolayout api in iOS 6.0. The layout that I am attempting to build has 2 buttons with a label inbetween them as shown in the image below:
What I would like is for the container view shown in grey to increase or decrease in size depending on the amount of text in the label.
So far I've managed to get the layout to work exactly as described with one small issue that I've not been able to resolve. The problem I am having is that the intrinsic height of the label is alway calculated with the original width of the label (e.g. the one set in interface builder) and not the width of the label that is set from the constraints I have on it.
The constraints that I have on the views are as follows:
H:|-[leftButton]-[label]-[rightButton]-|
Both buttons are centered Vertically
V: |-[label]-|
the content hugging priority of the buttons is higher than the label
the compression Resistance of the label is higher than the container view
All of these constraints together create the desired layout.
However:
If the Width of the label in interface builder (the literal size in interface builder) is less than the width of the view at runtime, then the height of the container is too large.
To help clarify this, the IB view of the above layout was as follows (note much narrower)
If the Width of the label in interface builder (the literal size in interface builder) is greater than the width of the view at runtime, then the height of the container is too small and lines of text get cut off.
What appears to be happening is that although there is no constraint on the labels width, the label seems to calculate its height (and thus the autolayout calculated height of the container) based on the original width of the label when it was created. However whats so strange is that the text appears to be the correct width and height but the label itself has a frame thats just too big or small.
I guess this is related to the order in which things are happening, and have tried calling updateConstraintsIfNeeded in the containerViews layoutSubview method however this doesn't appear to do anything.
Any suggestions would be greatly appreciated.
May be I am late but any way I've found solution. Any time when label width is changed you need to manually update preferredMaxLayoutWidth for Label. For example:
Label.preferredMaxLayoutWidth = 200;
Interface builder sets preferredMaxLayoutWidth with initial label width.
Hope this helps.
Have you tried calling -invalidateIntrinsicContentSize on your label?
Yes, -invalidateIntrinsicContentSize can also be used for this.
Make a Custom Class like 'CustomLabel'.
Set this Class for UILabel in interface builder.
Override the functions which returns the IntrinsicContentSize
-(CGSize)intrinsicContentSize {
//If the width is Zero.
if (self.frame.size.width == 0) {
return [super intrinsicContentSize];
}
return [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(self.frame.size.width, MAXFLOAT)];
}
Inform the system, to reCalculate it's Intrinsic Size by calling the method.
[CustomLabel invalidateIntrinsicContentSize];
Hope this would help.

Objective C - UITextView letter spacing and vertical spacing

How can I set the letter spacing and vertical spacing for a UITextView?
Trying to do it from nib if possible but if not, is there a property I can set through code?
Thank you,
Tee
There is no property within the UITextView to explicitly set the letter spacing, or vertical spacing - with the native controls it can't be done.
If you want to do this you're going to have to roll your own. There's a SO thread about changing the UILabel/UIFont letter spacing which ought to provide you with a direction to go in.
With that said, I have to ask the question why you want to do this? Apple is very specific about it's interface elements, and my thought is that tweaking a UITextView is going to be very off putting to your users.
As gavin has said, really; if possible you could change to a UILabel and set the lineBreakMode property, in conjunction with the contentSize property of the label's frame to partly achieve some light modification.
You could also replace spaces with a number of spaces for example from a string object, but again as has been said, I wouldn't advise tampering too far with this, especially if its going to be a public appstore project.
Good luck!
Well if you are looking for line height you can get like this.
yourUITextView.font.lineHeight
This would give you line height according to the current font size. This works perfectly in iOS 8. Not sure about backward compatibility.

Multiline CATextLayer

I'm trying to recreate the multiline UILabel with a CATextlayer.
Is there a property for CATextLayer, or do I need to create it myself?
I'm not sure what you mean exactly, but if you add a CATextLayer to a view, you can set it to have the same bounds as the view and if you enable the wrapped property on the CATextLayer, the text will word wrap. You can then also set the resizing mask on the layer so that changes to the view will be reflected in the layer.