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. enter image description here

+3


source to share


2 answers


Customizing the Text Fields article in the Text System UI Level Programming Guide:

enter image description here

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;

      

+12


source


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]; 

      

+1


source







All Articles