ios - 不兼容的指针类型将 UIFont 发送到 NSDictonary 类型的参数?

标签 ios nsdictionary sigabrt uifont

我得到了不兼容的指针类型,将 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.
}

最佳答案

崩溃 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];

关于ios - 不兼容的指针类型将 UIFont 发送到 NSDictonary 类型的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19440784/

相关文章:

iphone - 循环遍历包含字典数组的 plist

ios - 适用于 iOS 的 Paypal SDK

ios - 如何在 iOS 中使用图像创建连续动画

ios - WordPress iOS API

iphone - 是否有关于如何使用 OpenGL 纹理模糊的教程或简化示例?

ios - 应用 SIGABRT 崩溃

c++ - 程序收到信号 SIGABRT,中止

ios - 在 iOS 中同时插入 CoreData

ios - 无论如何在 Objective-C 中有一系列双对象

ios - 从 NSDictionary 获取值(value)不起作用