![](/img/trans.png)
[英]Calculate Height from NSAttributedString with NSParagraphStyle
[英]paragraphSpacingBefore from NSParagraphStyle does not work
NSParagraphStyle的段落SpacingBefore屬性似乎沒有任何作用。 我很確定它可以在以前的項目中使用,但是在我的舊代碼中看不到與現在所做的任何不同。 這是我的示例代碼:
textView.contentInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.firstLineHeadIndent = 22;
paragraphStyle.paragraphSpacingBefore = 120;
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[attributes setObject:[UIColor clearColor] forKey:NSBackgroundColorAttributeName];
[attributes setObject:[UIFont fontWithName:@"Helvetica" size:19] forKey:NSFontAttributeName];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." attributes:attributes];
任何建議將不勝感激。
謝謝。
您是否已將Interface Builder中的UITextView綁定到變量textView?
使用Objective-C文字 。
代碼已更新
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.backgroundColor = [UIColor grayColor];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.firstLineHeadIndent = 22;
paragraphStyle.paragraphSpacingBefore = 120;
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:13],
NSParagraphStyleAttributeName: paragraphStyle
};
self.textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." attributes:attributes];
}
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.