什么是iOS 7中的NSLineBreakMode等效的string绘制方法?

有一种方法

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment; 

我现在必须replaceiOS 7

 NSDictionary *attributes = @{NSFontAttributeName: font}; [self drawInRect:rect withAttributes:attributes]; 

但如何指定像换行一样的换行符模式? 我search了Attributedstring绘图符号的文档,但没有提到换行符模式。 这是自动的总是自动换行吗?

您需要创build一个段落样式。

 NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [style setLineBreakMode:NSLineBreakByWordWrapping]; NSDictionary *attributes = @{NSFontAttributeName: font, NSParagraphStyleAttributeName: style}; [self drawInRect:rect withAttributes:attributes]; 

更多信息在这里: https : //developer.apple.com/documentation/uikit/nsparagraphstyle?language=objc

在迅速:

 let style = NSMutableParagraphStyle() style.lineBreakMode = .ByWordWrapping let attributes: [String: AnyObject] = [ NSFontAttributeName: font, NSParagraphStyleAttributeName: style ]