The UIlabel AttributedString first line indentation - ios

label text first line need to indent,I use NSMutableParagraphStyle setFirstLineHeadIndent.
But when the label is only one line, AttributedString intercept omitted behind a few characters,why?
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
[style setFirstLineHeadIndent:11];
[[NSAttributedString alloc] initWithString:#"abcdefg" attributes:dic];

Related

UILabel with multiple chained NSAttributedString, with line limit, show the tail truncation with NSBackgroundColorAttributeName of unseen text

The dots are added automatically by the UILabel, cause of line limitation, but they get the background color of hidden truncated text:
So I have UILabel with line limit of 10 and line break mode of TruncatingTail.
I also have 2 types of attributed strings that build this UILabel content.
NSForegroundColorAttributeName, NSFontAttributeName
NSBackgroundColorAttributeName, NSForegroundColorAttributeName, NSFontAttributeName
Any idea why the UILabel is adding background color to the dots? There is text in line 12 (which is truncated) that have that background...
=Here Given Code Try it..
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:#"test"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
[text addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, text.length)];
label.attributedText = text;
It looks like your NSAttributedString styling is picking up on an instance of "Windsor" that is past the truncation point.
You could find where the truncation occurs, and then only apply the text attribute to the range of the string up to the truncation point.
See this SO answer to calculate this range.
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = mylabel.lineBreakMode;
NSDictionary *attributes = #{NSFontAttributeName : mylabel.font,
NSParagraphStyleAttributeName : paragraph};
CGSize constrainedSize = CGSizeMake(mylabel.bounds.size.width, NSIntegerMax);
CGRect rect = [mylabel.text boundingRectWithSize:constrainedSize
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes context:nil];
if (rect.size.height > mylabel.bounds.size.height) {
NSLog(#"TOO MUCH");
}

Only first NSParagraphStyle applied in NSAttributedString

Goal is to have a single NSAttributedString with a larger line height between paragraphs than within a paragraph, a fairly simple and common use case it seems to me. Here's my code:
NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
firstParagraphStyle.lineHeightMultiple = 3.0;
NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
secondParagraphStyle.lineHeightMultiple = 1.0;
NSAttributedString *title = [[NSAttributedString alloc] initWithString:#"Title"
attributes:#{NSParagraphStyleAttributeName: firstParagraphStyle}];
NSAttributedString *bodyTop = [[NSAttributedString alloc] initWithString:#"\u2029Body 1"
attributes:#{NSParagraphStyleAttributeName: secondParagraphStyle}];
NSAttributedString *bodyBottom = [[NSAttributedString alloc] initWithString:#"\u2029Body 2 line 1\u2028Body 2 line 2"
attributes:#{NSParagraphStyleAttributeName: secondParagraphStyle}];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:title];
[attributedString appendAttributedString:bodyTop];
[attributedString appendAttributedString:bodyBottom];
All four lines end up with the same line spacing of 3.0. In fact, when I remove the attributes dictionary entirely and simply do:
NSAttributedString *title = [[NSAttributedString alloc] initWithString:#"Title"];
NSAttributedString *bodyTop = [[NSAttributedString alloc] initWithString:#"\u2029Body 1"];
NSAttributedString *bodyBottom = [[NSAttributedString alloc] initWithString:#"\u2029Body 2 line 1\u2028Body 2 line 2"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:title];
[attributedString appendAttributedString:bodyTop];
[attributedString appendAttributedString:bodyBottom];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:firstParagraphStyle
range:NSMakeRange(0, 1)];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:secondParagraphStyle
range:NSMakeRange(1, attributedString.length - 1)];
it still renders all three paragraphs using line height multiple of 3.0. It seems that whatever the first paragraph style I apply to the string is, that's the one that applies to all subsequent lines and paragraphs!
Why doesn't using the special paragraph separator character \u2029 as Apple suggests here allow for more than one paragraph style within a single NSAttributedString? I'd prefer not to break into multiple UILabels.
Thanks in advance to anyone with deep Core Text knowledge on this subject.
Ended up getting this working. Turns out when I set alignment on the subsequent UILabel to .textAlignment = NSTextAlignmentCenter that messed up the paragraph style for the entire attributedText.
So the lesson is: If you're using multiple paragraph styles, don't set any corresponding properties on the UILabel or your behavior will be clobbered by the first paragraph style the label sees, even when the properties aren't related (e.g. line height and text alignment).

How to draw text in UITextView

How to draw text in UITextView just like there is a method called "drawTextInRect:" in UILabel and UITextField. Should I implement it in "drawRect:" or "drawLayer:inContext:". Thank your focus and patient!
you can use drawinrect like following code.
UIFont *font = [UIFont fontWithName:#"Courier" size:kCellFontSize];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentRight;
NSDictionary *attributes = #{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle };
[text drawInRect:rect withAttributes:attributes];

NSMutableParagraphStyle and NSAttributedString fail to wordrap

I am attempting to have a multiline NSAttributedString, with a line spacing of 1.25.
NSMutableParagraphStyle *bodyFormat = [[NSMutableParagraphStyle alloc] init];
bodyFormat.alignment = NSTextAlignmentLeft;
//[bodyFormat setLineSpacing:5];
[bodyFormat setLineBreakMode:NSLineBreakByWordWrapping];
//[bodyFormat setMaximumLineHeight:5];
[bodyFormat setLineHeightMultiple:1.25];
NSMutableAttributedString *desc = [[NSMutableAttributedString alloc] initWithString:#"So why to use Lorem Ipsum and why placeholder text is necessary? Naturally, page designs that are made for text documents must contain some text rather than placeholder dots or something else. Howevr, should they contain a proper English words and sentenses almost every reader will deliberately try to interpret it eventually missing the design itself"];
[desc addAttribute:NSParagraphStyleAttributeName value:bodyFormat range:NSMakeRange(0, desc.length)];
UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(0,
20,
300,
1000)];
description.lineBreakMode = NSLineBreakByWordWrapping;
[description setAttributedText:desc];
[self.view addSubview:description];
This manages to produce a single line, and the linebreak is not effective.
What am I doing wrong?
[bodyFormat setLineSpacing:5];
breaks this behavior.
Also setting
description.numberOfLines = 0;
helps... (description is the UILabel)

How Can I indent only first line of multiline UILabel in iOS?

I want to add an image in start of UILabel. Label is multiline. If I use contentInset, it indent the whole label but I want to indent first line only.
I have tried this so far, this doesn't work for me.
UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0, 40.0, 0.0, 0.0);
valueLabel.contentInset = titleInsets;
It should look like this.
#DavidCaunt suggestion worked for me. I am sharing code here.
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 50;
[attributedText addAttribute:NSParagraphStyleAttributeName value:style range:range];
[valueLabel setAttributedText:attributedText];
As a user716216 pointed, additionally - we can use a tab with defined indent value:
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.headIndent = 50;
label.attributedText = [[NSAttributedString alloc] initWithString:
#"\tHow can i add image like this in start of UILabel? Label is multiline.........."
attributes:#{NSParagraphStyleAttributeName: paragraphStyle}];
Here's how you can do this in Interface Builder:

Resources