drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

前端 未结 2 1364
一整个雨季
一整个雨季 2021-02-05 02:29

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:

相关标签:
2条回答
  • 2021-02-05 03:24

    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};
    
    0 讨论(0)
  • 2021-02-05 03: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?

    0 讨论(0)
提交回复
热议问题