Search code examples
iosobjective-cuitextviewnsattributedstring

NSAttributedString end of first line indent


I want to have the first line in an NSAttributedString for a UITextView indented from the right side on the first line.

So the firstLineHeadIndent in NSParagraphStyle will indent the first line from the left. I want to do the same thing but from the right in my UITextView.

Here's a screenshot of how I want the text to wrap.
enter image description here


Solution

  • The Setting Text Margins article from the Text System User Interface Layer Programming Guide has this figure:

    enter image description here

    As you can see, there's no built-in mechanism to have a first line tail indent.

    However, NSTextContainer has a property exclusionPaths which represents parts of its rectangular area from which text should be excluded. So, you could add a path for the upper-right corner to prevent text from going there.

    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;