如何使用drawAtPoint绘制多行文本?

时间:2011-06-26 21:16:38

标签: iphone objective-c cocoa-touch nsstring quartz-graphics

我是自定义绘制一些文字:

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

2 个答案:

答案 0 :(得分:7)

-drawAtPoint:withFont:...的文档说“此方法在绘图期间不执行任何换行。”如果您使用-drawInRect:withFont:代替-drawAtPoint:withFont:...,则会绘制多行。您还可以使用-sizeWithFont:constrainedToSize:来确定尺寸。

答案 1 :(得分:0)

您应该使用drawInRect:withFont:...

,而不是在iOS7 +中弃用drawWithRect:options:attributes:context:
[string drawWithRect:CGRectMake(x, y, width, height)
             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
          attributes:@{NSFontAttributeName:<font>, NSForegroundColorAttributeName:<color>
             context:nil];