简体   繁体   English

NSParagraphStyle的段SpacingBefore不起作用

[英]paragraphSpacingBefore from NSParagraphStyle does not work

The paragraphSpacingBefore property of NSParagraphStyle does not seem to do anything. NSParagraphStyle的段落SpacingBefore属性似乎没有任何作用。 I'm pretty sure it worked in a previous project, but I don't see anything different in my old code that what I'm doing now. 我很确定它可以在以前的项目中使用,但是在我的旧代码中看不到与现在所做的任何不同。 Here's my sample code: 这是我的示例代码:

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

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Thanks. 谢谢。

Have you binded your UITextView in the Interface Builder to your variable textView? 您是否已将Interface Builder中的UITextView绑定到变量textView?

Use Objective-C Literals . 使用Objective-C文字

Code Updated 代码已更新

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

}

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用NSParagraphStyle从NSAttributedString计算高度 - Calculate Height from NSAttributedString with NSParagraphStyle 如何从textView的attributedText中删除NSParagraphStyle属性? - How to remove an NSParagraphStyle Attribute from a textView's attributedText? NSAttributedString中仅应用了第一个NSParagraphStyle - Only first NSParagraphStyle applied in NSAttributedString 段落间距和段落间距之前有什么区别? (以NSMutableParagraphStyle形式) - What difference between paragraphSpacing and paragraphSpacingBefore? (in NSMutableParagraphStyle) UILabel无法与NSAttributedString + NSParagraphStyle一起使用,从右向左 - UILabel not working with NSAttributedString + NSParagraphStyle for Right-To-Left iOS为什么NSParagraphStyle重设原始文字样式 - iOS why NSParagraphStyle resets the original text styles NSURLSessionDataTask无法从unity插件工作 - NSURLSessionDataTask does not work from unity plugin NSParagraphStyle iOS - 如何检测块和列表? - NSParagraphStyle iOS - How to detect Blocks and Lists? UIScrollView setZoomscale在ViewWillApper中不起作用 - UIScrollView setZoomscale does not work from ViewWillApper sendSubviewToBack不适用于从xib创建的视图 - sendSubviewToBack does not work for a view created from xib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM