0

我收到不兼容的指针类型将 UIFont 发送到 NSDictonary 类型的参数,这让我得到一个信号 SIGABRT,我不知道这意味着什么,这是我的代码,我得到了错误......

- (void)drawDayNumber {
    if (self.selectionState == DSLCalendarDayViewNotSelected) {
    [[UIColor colorWithWhite:66.0/255.0 alpha:1.0] set];
    }
    else {
        [[UIColor whiteColor] set];
    }

    UIFont *textFont = [UIFont boldSystemFontOfSize:17.0];
    CGSize textSize = [_labelText sizeWithAttributes:textFont];//Im getting it here.

    CGRect textRect = CGRectMake(ceilf(CGRectGetMidX(self.bounds) - (textSize.width /2.0)), ceilf(CGRectGetMidY(self.bounds) - (textSize.height / 2.0)), textSize.width, textSize.height);
    [_labelText drawInRect:textRect withAttributes:textFont];//And here.
}
4

3 回答 3

1

崩溃 1

问题在于此代码:

[_labelText drawInRect:textRect withAttributes:textFont];

方法的第二个参数drawInRect:withAttributes:期望NSDictionary,您正在传递UIFont对象。这就是崩溃的原因。

您应该执行以下操作:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: textFont,NSFontAttributeName,nil];
[_labelText drawInRect:textRect withAttributes:dict];

或者

您可以改用:drawInRect:withFont方法。

[_labelText drawInRect:rect withFont:textFont];

崩溃 2

在以下代码中:

CGSize textSize = [_labelText sizeWithAttributes:textFont];

这里的参数也是错误的,sizeWithAttributes:方法期望NSDictionary,你正在传递UIFont对象。

将其更改为:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: textFont,NSFontAttributeName,nil];
CGSize textSize = [_labelText sizeWithAttributes:dict];
于 2013-10-18T02:53:05.160 回答
1

您应该为属性创建一个字典:

NSDictionary *attributes = @{NSFontAttributeName : textFont};

您应该将此attributes字典用作这些方法的参数。如果您也想设置字体颜色,您可以执行以下操作:

UIColor *color;
if (self.selectionState == DSLCalendarDayViewNotSelected)
    color = [UIColor colorWithWhite:66.0/255.0 alpha:1.0];
else
    color = [UIColor whiteColor];

UIFont *textFont = [UIFont boldSystemFontOfSize:17.0];

NSDictionary *attributes = @{NSFontAttributeName            : textFont,
                             NSForegroundColorAttributeName : color};

CGSize textSize = [_labelText sizeWithAttributes:attributes];

CGRect textRect = CGRectMake(ceilf(CGRectGetMidX(self.bounds) - (textSize.width /2.0)), ceilf(CGRectGetMidY(self.bounds) - (textSize.height / 2.0)), textSize.width, textSize.height);
[_labelText drawInRect:textRect withAttributes:attributes];

请参阅NSAttributedString Application Kit Additions Reference以获取您可以在NSDictionary其中用于属性的各种键的列表。

于 2013-10-18T02:56:03.847 回答
0

您正在将一个UIFont对象传递给一个需要字典的方法。您需要创建一个字典,使用您希望设置的任何键。

你哪里可能想到了这个方法。

[_labelText sizeWithFont:font];
于 2013-10-18T02:50:40.247 回答