【问题标题】:text alignment drawInRect Not Working文本对齐drawInRect不起作用
【发布时间】:2013-04-30 08:01:18
【问题描述】:

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

【问题讨论】:

  • 您应该使用 NSLineBreakByWordWrappingNSTextAlignmentRight,因为 UI 等效项已被弃用。它们映射到同一个东西,所以它不应该有所作为。
  • "不工作" 对于对齐。我使用了 UI 等效项(NSLineBreakByWordWrapping 和 NSTextAlignmentRight),但仍然无法正常工作!!文本仍在中心。

标签: ios objective-c xcode drawrect


【解决方案1】:

您是否检查了您传递给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]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2012-07-30
    • 1970-01-01
    • 2012-01-25
    • 2011-03-24
    • 2012-09-12
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多