Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

I'm working on a new version of my app and am attempting to replace deprecated messages, but am not able to get past this one.

I can't figure out why drawInRect:withAttributes is not working. The code displays properly when drawInRect:withFont:lineBreakMode:alignment message is sent, but does not work when drawInRect:withAttributes is sent.

I'm using the same rect and font and I what I believe is the same text style. The constants are just positioning the rect just below an image, but I'm using the same rect for both calls, so I'm certain the rectangle is correct.

(note that bs.name used below is an NSString object)

        CGRect textRect = CGRectMake(fCol*kRVCiPadAlbumColumnWidth,
                                     kRVCiPadAlbumColumnWidth-kRVCiPadTextLabelYOffset,
                                     kRVCiPadAlbumColumnWidth,
                                     kRVCiPadTextLabelHeight);
        NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        textStyle.lineBreakMode = NSLineBreakByWordWrapping;
        textStyle.alignment = NSTextAlignmentCenter;
        UIFont *textFont = [UIFont systemFontOfSize:16];

This doesn't work (nothing is drawn on the screen) using the variables from above

        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];

This Does work (the string is drawn properly on the screen) using the same variables from above

        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];

Any assistance would be great. Thanks.

like image 520
Ross Pirtle Avatar asked Oct 09 '13 14:10

Ross Pirtle


2 Answers

To set the color of text you need to pass the NSForegroundColorAttributeName in the attribute as the additional parameter.

NSDictionary *dictionary = @{ NSFontAttributeName: self.font,
                              NSParagraphStyleAttributeName: paragraphStyle,
                              NSForegroundColorAttributeName: self.textColor};
like image 59
Hiren Avatar answered Nov 18 '22 03:11

Hiren


I've made a UIView with drawRect: containing only the code you provided

- (void)drawRect:(CGRect)frame
{
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    textStyle.alignment = NSTextAlignmentCenter;
    UIFont *textFont = [UIFont systemFontOfSize:16];

    NSString *text = @"Lorem ipsum";

    // iOS 7 way
    [text drawInRect:frame withAttributes:@{NSFontAttributeName:textFont, NSParagraphStyleAttributeName:textStyle}];

    // pre iOS 7 way
    CGFloat margin = 16;
    CGRect bottomFrame = CGRectMake(0, margin, frame.size.width, frame.size.height - margin);
    [text drawInRect:bottomFrame withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}

I don't see any difference between the outputs of these two methods. Maybe the problem is somewhere else in your code?

like image 41
Arek Holko Avatar answered Nov 18 '22 03:11

Arek Holko