我希望NSAttributedString
中的第一行为UITextView
从第一行右侧缩进。
因此firstLineHeadIndent
中的NSParagraphStyle
将从左侧缩进第一行。我想做同样的事情,但是从我UITextView
的右边开始。
这是我希望文本包装的屏幕截图
答案 0 :(得分:14)
“文本系统用户界面层编程指南”中的Setting Text Margins article具有此图:
正如您所看到的,没有内置机制可以使第一行尾部缩进。
但是,NSTextContainer
有一个属性exclusionPaths
,它表示应从中排除文本的矩形区域的一部分。因此,您可以为右上角添加一个路径,以防止文本进入那里。
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;
答案 1 :(得分:1)
我建议创建2个不同的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];