Question

I got incompatible pointer types sending UIFont to parameter of type NSDictonary and it is making me get a signal SIGABRT and i don't know what it means here is my code that im getting the error in...

- (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.
}
Was it helpful?

Solution

Crash 1

Issue is with this code:

[_labelText drawInRect:textRect withAttributes:textFont];

2nd parameter of drawInRect:withAttributes: method expects NSDictionary, you are passing UIFont object. That is the reason for the crash.

You should do something like:

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

or

You can use: drawInRect:withFont method instead.

[_labelText drawInRect:rect withFont:textFont];

Crash 2

In the following code:

CGSize textSize = [_labelText sizeWithAttributes:textFont];

here also the parameter is wrong, sizeWithAttributes: method expects NSDictionary, you are passing UIFont object.

Change that to:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: textFont,NSFontAttributeName,nil];
CGSize textSize = [_labelText sizeWithAttributes:dict];

OTHER TIPS

You should create a dictionary for the attributes:

NSDictionary *attributes = @{NSFontAttributeName : textFont};

You should use this attributes dictionary as the parameter for these methods. If you want to set the font color, too, you might do the following:

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

See the NSAttributedString Application Kit Additions Reference for a list of the various keys you can use in this NSDictionary for the attributes.

You are passing in a UIFont object to a method that requires a Dictionary. You need to create a dictionary with which ever keys you wish to set.

You where probably thinking of this method.

[_labelText sizeWithFont:font];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top