如何使用drawAtPoint绘制多行文本?
我正在自定义绘制一些文本:
point = CGPointMake(77, 5);
[[message valueForKey:@"user_login"] drawAtPoint:point forWidth:200
withFont:mainFont
minFontSize:MIN_MAIN_FONT_SIZE
actualFontSize:NULL
lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
如何让它绘制 5 条线?相当于:
rect = CGRectMake(77, 25, 238, 68);
bodyLabel = [[UILabel alloc] initWithFrame:rect];
bodyLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
bodyLabel.numberOfLines = 5;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
bodyLabel.textColor = [UIColor blackColor];
[self.contentView addSubview: bodyLabel];
I am custom drawing some text:
point = CGPointMake(77, 5);
[[message valueForKey:@"user_login"] drawAtPoint:point forWidth:200
withFont:mainFont
minFontSize:MIN_MAIN_FONT_SIZE
actualFontSize:NULL
lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
How can I make it draw 5 lines? Equivalent to:
rect = CGRectMake(77, 25, 238, 68);
bodyLabel = [[UILabel alloc] initWithFrame:rect];
bodyLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
bodyLabel.numberOfLines = 5;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
bodyLabel.textColor = [UIColor blackColor];
[self.contentView addSubview: bodyLabel];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-drawAtPoint:withFont:...
的文档说“此方法在绘图过程中不执行任何换行。”如果您使用-drawInRect:withFont:
而不是-drawAtPoint:withFont:...
,那么它将绘制多条线。您还可以使用 -sizeWithFont:constrainedToSize: 来确定大小。The documentation for
-drawAtPoint:withFont:...
says "This method does not perform any line wrapping during drawing." If you use-drawInRect:withFont:
instead of-drawAtPoint:withFont:...
, then it will draw multiple lines. You can also use-sizeWithFont:constrainedToSize:
to figure out what the size will be.您应该使用
drawWithRect:options:attributes:context:
,而不是 iOS7+ 中已弃用的drawInRect:withFont:...
Instead of deprecated
drawInRect:withFont:...
in iOS7+ you should usedrawWithRect:options:attributes:context: