2011-06-26 28 views
3

我自定義繪製一些文字:如何用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]; 

回答

6

-drawAtPoint:withFont:...文檔說「繪製過程中,該方法不執行任何換行。」如果您使用-drawInRect:withFont:而不是-drawAtPoint:withFont:...,那麼它將繪製多行。您也可以使用-sizeWithFont:constrainedToSize:來確定尺寸。

0

而不是在iOS7 +棄用drawInRect:withFont:...你應該使用drawWithRect:options:attributes:context:

[string drawWithRect:CGRectMake(x, y, width, height) 
      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine 
      attributes:@{NSFontAttributeName:<font>, NSForegroundColorAttributeName:<color> 
      context:nil];