有一种方法
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;我现在必须将其替换为iOS 7。
NSDictionary *attributes = @{NSFontAttributeName: font};
[self drawInRect:rect withAttributes:attributes];但是如何指定换行模式,如换行?我搜索了属性字符串绘制符号的文档,但没有提到换行模式。这是自动的自动换行吗?
发布于 2013-10-05 20:38:21
您需要创建段落样式。
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
发布于 2016-04-07 16:21:27
在Swift 5中:
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byWordWrapping
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.paragraphStyle: style
]https://stackoverflow.com/questions/19197709
复制相似问题