Ios 文本对齐drawInRect不工作

Ios 文本对齐drawInRect不工作,ios,objective-c,xcode,drawrect,Ios,Objective C,Xcode,Drawrect,我有下面的代码在UITableViewCell中绘制文本,它可以很好地绘制并返回文本,但位于UITableViewCell的中心。所以我给了它正确的校准,但没有工作 UIColor * textColor = [UIColor blackColor]; if (self.selected || self.highlighted){ textColor = [UIColor whiteColor]; } else { [[UIColor whiteColor] set];

我有下面的代码在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]; 

您是否检查了传递给
drawInRect:
CGRect
?您正在
rect
的中心显式创建一个
CGRect
。应该是:

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

您是否检查了传递给
drawInRect:
CGRect
?您正在
rect
的中心显式创建一个
CGRect
。应该是:

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

您应该使用
NSLineBreakByWordWrapping
NSTextAlignmentRight
,因为UI等价物已被弃用。它们映射到同一个对象,所以不会有什么不同,但是对对齐来说“不起作用”。我使用了UI等价物(NSLineBreakByWordWrapping和NSTextAlignmentRight),但仍然不起作用!!文本仍在中间。您应该使用
NSLineBreakByWordWrapping
NSTextAlignmentRight
,因为用户界面等价物已被弃用。它们映射到同一个对象,所以不会有什么不同,但是对对齐来说“不起作用”。我使用了UI等价物(NSLineBreakByWordWrapping和NSTextAlignmentRight),但仍然不起作用!!文本仍在中间。