如何在放置在UIToolbar中时更改UIBarButtonItem的标题颜色?

时间:2012-09-25 15:44:10

标签: ios

我已尝试设置标题文字属性。使用图标设置Button,然后设置title属性。图标正确着色。标题仍然是“未标记的”

附加截图。enter image description here

2 个答案:

答案 0 :(得分:0)

如果tintColor不影响文本颜色,我所知道的唯一解决方案是基本上创建图标和文本的合成图像,然后将其用作按钮图像:

UIGraphicsBeginImageContextWithOptions(someSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// draw the image
// Use UIKit NSString additions to draw the text

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:0)

大卫的建议有效。以下是示例代码:

 + (UIImage *) image:(NSString *) imageName withTitle:(NSString *)title {

     UIFont *titleFont = [UIFont boldSystemFontOfSize:10];
     CGSize textSize = [title sizeWithFont:titleFont];

     CGFloat width = MAX(35, textSize.width);
     UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, 35), NO, 0);
     CGContextRef context = UIGraphicsGetCurrentContext();

     UIImage *toolbarImage = [UIImage imageNamed:imageName];
     CGContextSaveGState(context);  {
         CGContextTranslateCTM(context, 0, 35);
         CGContextScaleCTM(context, 1.0, -1.0);
         CGContextDrawImage(context, CGRectMake((width - toolbarImage.size.width) / 2, 14, toolbarImage.size.width, toolbarImage.size.height), toolbarImage.CGImage);
     }
     CGContextRestoreGState(context);

     [title drawInRect:CGRectMake((width - textSize.width) / 2, 22, textSize.width, textSize.height) withFont:titleFont];

     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

return image;

}

调整一些帧参数以适合您的图像。