NSAttributedString end of indentation of first line
I want to have the first line in NSAttributedString
for UITextView
right-indented on the first line.
So, firstLineHeadIndent
in the NSParagraphStyle
will to retreat the first row on the left. I want to do the same but on the right side of the UITextView
.
Here is a screenshot of how I want the text to be wrapped.
source to share
Customizing the Text Fields article in the Text System UI Level Programming Guide:
As you can see, there is no built-in mechanism to indent the first tail of a line.
However, it NSTextContainer
has a property exclusionPaths
that represents portions of its rectangular area from which text should be excluded. This way you can add a path for the top right corner to prevent the text from moving.
UIBezierPath* path = /* compute path for upper-right portion that you want to exclude */;
NSMutableArray* paths = [textView.textContainer.exclusionPaths mutableCopy];
[paths addObject:path];
textView.textContainer.exclusionPaths = paths;
source to share
I would suggest creating 2 different ones NSParagraphStyle
, one for the first line and one for the rest of the text.
//Creating first Line Paragraph Style
NSMutableParagraphStyle *firstLineStyle = [[NSMutableParagraphStyle alloc] init];
[firstLineStyle setFirstLineHeadIndent:10];
[firstLineStyle setTailIndent:200]; //Note that according to the doc, it in point, and go from the origin text (left for most case) to the end, it more a length that a "margin" (from right) that why I put a "high value"
//Read there: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/index.html#//apple_ref/occ/instp/NSMutableParagraphStyle/tailIndent
//Creating Rest of Text Paragraph Style
NSMutableParagraphStyle *restOfTextStyle = [[NSMutableParagraphStyle alloc] init];
[restOfTextStyle setAlignement:NSTextAlignmentJustified];
//Other settings if needed
//Creating the NSAttributedString
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:originalString];
[attributedString addAttribute:NSParagraphStyleAttributeName value:firstLineStyle range:rangeOfFirstLine];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:restOfTextStyle
range:NSMakeRange(rangeOfFirstLine.location+rangeOfFirstLine.length,
[originalString length]-(rangeOfFirstLine.location+rangeOfFirstLine.length))];
//Setting the NSAttributedString to your UITextView
[yourTextView setAttributedText:attributedString];
source to share