1

我正在尝试使用两行文本创建一个 UIBarButtonItem,例如 iPod.app 中的“正在播放”按钮

我在这个线程中使用提供的代码:我们如何在导航栏中的 UIBarButtonItem 中放置两行

但在设备上运行时,该按钮以低分辨率显示,如下所示:

https://www.dropbox.com/s/vp5vn9mmq0f96n8/2012-08-21%2014.57.1​​2.png

为什么?我不想使用静态图像,因为我的应用程序已本地化。

4

1 回答 1

1

要解决此问题,如果设备是视网膜,请使用 UIGraphicsBeginImageContextWithOptions 而不是 UIGraphicsBeginImageContext。然后将上下文的比例设置为 mainScreen 比例。

+ (UIImage *)nowPlayingButton {

    UILabel * addCustomLabel = 
            [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 25)];
    addCustomLabel.text = 
        NSLocalizedString(@"NOW_PLAYING_NEWLINE", 
                          @"Now playing text divided on two lines");
    addCustomLabel.textColor = [UIColor whiteColor];
    addCustomLabel.font = [UIFont boldSystemFontOfSize:10];
    addCustomLabel.numberOfLines = 2;
    addCustomLabel.backgroundColor = [UIColor clearColor];
    addCustomLabel.textAlignment = UITextAlignmentCenter;

    CGSize size = addCustomLabel.bounds.size;

    static CGFloat scale = -1.0;

    UIScreen *screen = [UIScreen mainScreen];
    scale = [screen scale];

    if(scale > 0.0) {
        UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    } else {
        UIGraphicsBeginImageContext(size);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();
    [addCustomLabel.layer renderInContext: context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRelease(context);

    return img;
}
于 2012-08-23T16:47:23.447 回答