I have some UILabel that contains a couple of different fonts, which I apply via attributedText.
I have set the lineBreakMode to be UILineBreakModeWordWrap and I have set the size of the UILabel to be the sizeThatFits
And it looks just fine, until I add NSBaselineOffsetAttributeName to it's attributedText
Then I see weird truncation, regardless of how I change the height or width of the UILabel
How do I alter the baseline without affecting truncation?
I took the advice from the post, iOS Why do NSTextAttachment disappear when setting NSBaselineOffsetAttributeName?
and added a new NSMutableParagraphStyle to the the UILabel's attributedText with it's minimumLineHeight set to be the font's size plus the added baseline, and I got the results I was looking for.
Related
I need to decrease line spacing on interface builder using attributed text, and changing the line height multiplier it works as desired, but the text is being cropped on top.
I already tried to set the lineSpacing with NSMutableParagraphStyle by code but did not worked or happened the same thing.
There is a way to fix the alignment or setting the correctly the line spacing?
So it isn't a matter of spacing, but more likely constraints. Perhaps your baseline setting is set to none, in which case try changing it to 'Align Centers'.
In interfaceBuilder i have created a simple UILabel. It has text inside and i set it to attributedText. I'm not able to change the font size. Each time i try to change the font and run the program the font remains unchanged. In fact what i have tried is after i alter the font size and run the app and return to IB the font has been reset on its own !!
here are my IB settings for UILabel attributedText:
Select the text you want to modify
For example : Selected the "platinum" then reset font
Try this:
yourlabel.font = [yourlabel.font fontWithSize:16];
or
[yourlabel setFont:[UIFont systemFontOfSize:16]];
Setting the font size is better done in your code, typically in viewWillAppear or viewDidLoad methods.
This link --> HERE <-- has another way by creating a method to handle changing font size in attributed text. Not bad either.
I am using a UILabel within a tableViewCell. Some of the cells have texts that are one line, and some 2 lines. I have set the trailing space, leading space and top space from superview constraints to the label. Following are the settings:
lines = 0
Line Breaks: Word Wrap (tried other options as well)
If the text crosses a particular length, for some reason, the label height seems to have increased to accommodate 2 lines, though the text is only one line. As show below (background is red for reference):
For example, In the first row, the actual text is one line, but the height is increased when compared to the other rows.
Is there any reason why it might be happening so? I am not setting the height anywhere from the code.
EDIT
Also, I have set a custom font for UILabel throughout out the App using the following:
[[UILabel appearance] setFont:myFont];
I am not sure if that should cause any issue..
Also, the screen shot of the constraints set are:
Got the same issue.
After checking content hugging priorities got the same results.
Finally found that it was caused by Label-Preferred Width set to Explicit in Metrics Tab, in label configuration.
Unchecked and solved =)
I know its weird, I did face the same issue before and it was because of the content hugging priorities which i set without much knowledge on it. I solved it by removing the label and adding it again.try that.
I was experiencing this issue with an NSAttributedString, but in conjunction with using the lineSpacing property of NSMutableParagraphStyle. I'm using Auto Layout entirely programmatically and with numberOfLines set to 0. Multiline text was working fine until I wanted to add line spacing, at which time I encountered this issue. Adjusting the content hugging and compression resistance didn't do anything for me, nor did using UILabel's preferredMaxLayoutWidth property (I'm not currently).
What fixed it was applying an NSRange to the exact amount of characters in my text string:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = spacing;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
label.attributedText = attributedString;
Taken from this answer: https://stackoverflow.com/a/16057159/482557.
I need to increase the line spacing in a UILabel, so I’m using its attributedString to do so. Works fine, but it ignores the fact that I had also set the label’s numberOfLines to 2 – it only displays a single line, and truncates.
Any idea why this might be, what I can do about it, to still use two lines, but increase the spacing between them?
It sounds like the frame of your UILabel isn't big enough. Try increasing the height and see if the problem persists.
You can force the label to the correct size for your number of lines by calling sizeToFit after setting the attributedText. To avoid the problem of the shrinking label in your table view cells, reset the frame of the label to some default size before setting the attributedText and calling sizeToFit. This will prevent the label from continuously shrinking.
As far as I know, laying out paragraphs can be done with:
a) UITextView
Resizing the textview to fit the content in didlayoutsubviews
Example here: Weird thing occurs when using sizetofit of a UITextView in IOS7
b) UILabel
Setting the Lines to 0, using sizetofit
Example here: Multiple lines of text in UILabel
However, I am unable to get either of these to work in IOS7 (having previously used them in ios6.x). There must be a definitive and clear way to just lay out a paragraph, its such a seemingly simple task.To be specific, this is just a paragraph of text that is:
non-editable
variable length
Works consistently whether using storyboards or code only
So please, what is the way to do this?
UITextView works fine on iOS 7. If you don't use Auto Layout, then calling sizeToFit on UITextView object should be enough. If you do use Auto Layout, then make a height constraint on UITextView object and set its constant in code in the following way:
CGSize sizeThatFits = [self.textView sizeThatFits:CGSizeMake(yourAvailableWidth, MAXFLOAT)];
self.textViewHeightConstraint.constant = ceilf(sizeThatFits.height);
I've seen some problems with UILabel recently, e.g. Lines missing from tall UILabel when embedding NSTextAttachment
With the UILabel, I was able to to get this working by:
setting the lines to 0
setting my line break mode to word wrap
ensuring that the height constraint is set to a "greater than or equal to"