在iOS 7中NSLineBreakMode的等效字符串绘制方法是什么?

ope*_*rog 29 nsstring uikit nsattributedstring ios

有一种方法

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
Run Code Online (Sandbox Code Playgroud)

我现在必须替换iOS 7.我得到了

NSDictionary *attributes = @{NSFontAttributeName: font};
[self drawInRect:rect withAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)

但如何指定像换行一样的换行模式?我搜索了归因字符串绘制符号的文档但没有提到换行模式.这自动化总是自动换行吗?

Leo*_*ica 55

您需要创建一个段落样式.

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];

NSDictionary *attributes = @{NSFontAttributeName: font, NSParagraphStyleAttributeName: style};
[self drawInRect:rect withAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)

更多信息请访问:https: //developer.apple.com/documentation/uikit/nsparagraphstyle?language = objc


Phl*_*man 8

在快速:

let style = NSMutableParagraphStyle()
style.lineBreakMode = .byWordWrapping

let attributes: [NSAttributedString.Key: Any] = [
   .font: font,
   .paragraphStyle: style
]
Run Code Online (Sandbox Code Playgroud)