NSAttributedString第一行缩进结束

我想在第一行的右边缩进UITextViewNSAttributedString的第一行。

因此, NSParagraphStylefirstLineHeadIndent将从左侧缩进第一行。 我想做同样的事情,但从我的UITextView的权利。

这里是我想如何包装文本的截图。
在这里输入图像说明

“文本系统用户界面层编程指南”中的“ 设置文本边距”文章包含此图:

在这里输入图像说明

正如你所看到的,没有内置的机制来进行第一行尾缩进。

但是, NSTextContainer有一个属性exclusionPaths ,它表示从中排除文本的矩形区域的一部分。 所以,你可以添加一个右上angular的path,以防止文本去那里。

 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; 

我build议创build2个不同的NSParagraphStyle :一个特定的第一行和第二个其余的文本。

  //Creating first Line Paragraph Style NSMutableParagraphStyle *firstLineStyle = [[NSMutableParagraphStyle alloc] init]; [firstLineStyle setFirstLineHeadIndent:10]; [firstLineStyle setTailIndent:200]; //Note that according to the doc, it's in point, and go from the origin text (left for most case) to the end, it's more a length that a "margin" (from right) that's 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];