我试过设置标题文本属性。Button 是通过使用图标设置的,然后设置 title 属性。图标颜色正确。标题仍然“未着色”
附上截图。
大卫的建议奏效。这是示例代码:
+ (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;
}
调整一些框架参数以适合您的图像。
如果 tintColor 不影响文本颜色,我所知道的唯一解决方案是本质上创建图标和文本的合成图像,然后将其用作按钮图像:
UIGraphicsBeginImageContextWithOptions(someSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the image
// Use UIKit NSString additions to draw the text
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();