小编Ros*_*tle的帖子

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

我正在开发我的应用程序的新版本,并且我正在尝试替换已弃用的消息,但我无法通过这个消息.

我无法弄清楚为什么drawInRect:withAttributes不起作用.drawInRect:withFont:lineBreakMode:alignment发送消息时代码正确显示,但drawInRect:withAttributes发送时不起作用.

我使用相同的矩形和字体,我相信是相同的文本样式.常量只是将rect定位在图像下方,但我对两个调用使用相同的rect,所以我确定矩形是正确的.

(注意下面使用的bs.name是一个NSString对象)

        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];
Run Code Online (Sandbox Code Playgroud)

使用上面的变量,这不起作用(屏幕上没有任何内容)

        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];
Run Code Online (Sandbox Code Playgroud)

这可以使用上面相同的变量工作(在屏幕上正确绘制字符串)

        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];
Run Code Online (Sandbox Code Playgroud)

任何援助都会很棒.谢谢.

objective-c nsstring ios drawinrect

30
推荐指数
2
解决办法
3万
查看次数

标签 统计

drawinrect ×1

ios ×1

nsstring ×1

objective-c ×1