drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment
Asked Answered
H

2

30

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.

Higbee answered 9/10, 2013 at 14:8 Comment(2)
What do you mean by saying that drawInRect:withAttributes: doesn't work?Demolition
Very good question. Sorry I didn't elaborate. The string that is contained in bs.name is drawn correctly on the screen using drawInRect:withFont:lineBreakMode:alignment message but does not appear when using drawInRect:withAttributes message (nothing is drawn).Higbee
C
37

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};
Conga answered 10/10, 2013 at 10:18 Comment(0)
D
30

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?

Demolition answered 9/10, 2013 at 15:31 Comment(4)
Thanks for your work in providing this answer. My code is also executed from a drawRect: on a UIView. I agree it must be somewhere else, but if I comment out one or the other, one works the other doesn't. Maybe the rectangle needs to be recalculated, for whatever reason, different on iOS 7 (just for info, the iOS 7 way is drawInRect:withAttributes). I'll mess around with the rectangle a bit more. Thanks again.Higbee
Make sure CGRect's passed to both methods are the same. You can also try to 1) change the text color, 2) draw a background on your rectangle, just to make sure it's positioned where you want it to be. These steps are rather simplistic, but maybe they'll help to find where's the problem.Demolition
I'm using the exact same code in exactly the same method (not two methods). If you replace drawRect:(old) with drawRect:(new) in exactly the same spot in the code using the exact same rectangle, one works, one doesn't. I'm still stumped. If I find anything, I'll post.Higbee
OK, I figured it out and it was kind of a duh (as is probably the case most times). Thanks for the suggestion! I was using: [[UIColor lightGrayColor] set]; to set the the text color and this works with the earlier version of drawInRect: In the newer version, you set the text color as an attribute. So it was working, it was just drawing black text on a black background. I'm unable to post an answer because I'm too new of a user.Higbee

© 2022 - 2024 — McMap. All rights reserved.