1

我有以下代码在 UITableViewCell 中绘制文本,它可以正常绘制并返回文本,但在 UITableViewCell 的中心。所以我给它右对齐但不工作!

UIColor * textColor = [UIColor blackColor];
if (self.selected || self.highlighted){
    textColor = [UIColor whiteColor];
}
else
{
    [[UIColor whiteColor] set];
    UIRectFill(self.bounds);
}

[textColor set];

UIFont * textFont = [UIFont boldSystemFontOfSize:14];

CGSize textSize = [text sizeWithFont:textFont constrainedToSize:rect.size];

[text drawInRect:CGRectMake((rect.size.width / 2) - (textSize.width / 2),
                            (rect.size.height / 2) - (textSize.height / 2),
                            textSize.width, 
                            textSize.height) 
        withFont:textFont 
   lineBreakMode:UILineBreakModeWordWrap 
       alignment:UITextAlignmentRight]; 
4

1 回答 1

1

你检查过CGRect你传递给的drawInRect:吗?您明确地CGRectrect. 它应该是:

[text drawInRect:CGRectMake(rect.origin.x,
                            (rect.size.height / 2) - (textSize.height / 2),
                            rect.size.width, textSize.height) 
        withFont:textFont 
   lineBreakMode:UILineBreakModeWordWrap 
       alignment:UITextAlignmentRight]; 
于 2013-04-30T13:25:11.950 回答