[英]Color Tint UIButton Image
我注意到,當我將白色或黑色UIImage
放入UISegmentedControl
時,它會自動對其進行顏色遮罩以匹配分段控件的色調。 我認為這真的很酷,並且想知道我是否也可以在其他地方這樣做。 例如,我有一堆形狀統一但形狀各異的按鈕 colors。我不是為每個按鈕制作一個 PNG,而是可以以某種方式使用此顏色遮罩為所有按鈕使用相同的圖像,然后設置色調顏色或改變它們實際顏色的東西?
從 iOS 7 開始,在UIImage
上有一個新方法來指定渲染模式。 使用渲染模式UIImageRenderingModeAlwaysTemplate
將允許圖像顏色由按鈕的色調顏色控制。
目標-C
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [[UIImage imageNamed:@"image_name"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
迅速
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red
您必須將圖像渲染模式設置為UIImageRenderingModeAlwaysTemplate
以使tintColor
影響 UIImage。 這是 Swift 中的解決方案:
let image = UIImage(named: "image-name")
let button = UIButton()
button.setImage(image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
button.tintColor = UIColor.whiteColor()
快速 4 倍
button.setImage(image.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
button.tintColor = UIColor.blue
在 Swift 中,你可以這樣做:
var exampleImage = UIImage(named: "ExampleImage.png")?.imageWithRenderingMode(.AlwaysTemplate)
然后在你的 viewDidLoad
exampleButtonOutlet.setImage(exampleImage, forState: UIControlState.Normal)
並修改顏色
exampleButtonOutlet.tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) //your color
編輯 Xcode 8現在您還可以將 .xcassets 中的圖像渲染模式設置為模板圖像,然后您不再需要在var exampleImage
專門聲明它
不確定您想要什么,但此類別方法將使用指定顏色屏蔽 UIImage,因此您可以擁有單個圖像並將其顏色更改為您想要的任何顏色。
圖像實用程序
- (UIImage *) maskWithColor:(UIColor *)color;
ImageUtils.m
-(UIImage *) maskWithColor:(UIColor *)color
{
CGImageRef maskImage = self.CGImage;
CGFloat width = self.size.width;
CGFloat height = self.size.height;
CGRect bounds = CGRectMake(0,0,width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClipToMask(bitmapContext, bounds, maskImage);
CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
CGContextFillRect(bitmapContext, bounds);
CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
UIImage *coloredImage = [UIImage imageWithCGImage:cImage];
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cImage);
return coloredImage;
}
導入 ImageUtils 類別並執行以下操作...
#import "ImageUtils.h"
...
UIImage *icon = [UIImage imageNamed:ICON_IMAGE];
UIImage *redIcon = [icon maskWithColor:UIColor.redColor];
UIImage *blueIcon = [icon maskWithColor:UIColor.blueColor];
帶有 customType 的 Swift 4:
let button = UIButton(frame: aRectHere)
let buttonImage = UIImage(named: "imageName")
button.setImage(buttonImage?.withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
對於 Xamarin.iOS (C#):
UIButton messagesButton = new UIButton(UIButtonType.Custom);
UIImage icon = UIImage.FromBundle("Images/icon.png");
messagesButton.SetImage(icon.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal);
messagesButton.TintColor = UIColor.White;
messagesButton.Frame = new RectangleF(0, 0, 25, 25);
斯威夫特3 :
如果您已經通過 xCode 界面構建器設置了圖像,則此解決方案可能會很舒服。 基本上你有一個擴展來着色圖像:
extension UIImage {
public func image(withTintColor color: UIColor) -> UIImage{
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(CGBlendMode.normal)
let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context.clip(to: rect, mask: self.cgImage!)
color.setFill()
context.fill(rect)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
然后,您可以准備此UIButton擴展來為特定狀態的圖像着色:
extension UIButton {
func imageWith(color:UIColor, for: UIControlState) {
if let imageForState = self.image(for: state) {
self.image(for: .normal)?.withRenderingMode(.alwaysTemplate)
let colorizedImage = imageForState.image(withTintColor: color)
self.setImage(colorizedImage, for: state)
}
}
}
用法:
myButton.imageWith(.red, for: .normal)
PS(在表格單元格中也很好用,你不需要調用setNeedDisplay()
方法,由於UIImage擴展,顏色的變化是立即的..
如果您想手動屏蔽圖像,這里是適用於視網膜屏幕的更新代碼
- (UIImage *)maskWithColor:(UIColor *)color
{
CGImageRef maskImage = self.CGImage;
CGFloat width = self.size.width * self.scale;
CGFloat height = self.size.height * self.scale;
CGRect bounds = CGRectMake(0,0,width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextClipToMask(bitmapContext, bounds, maskImage);
CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
CGContextFillRect(bitmapContext, bounds);
CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
UIImage *coloredImage = [UIImage imageWithCGImage:cImage scale:self.scale orientation:self.imageOrientation];
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cImage);
return coloredImage;
}
你應該試試
設置框架后
NSArray *arr10 =[NSArray arrayWithObjects:btn1,btn2,nil];
for(UIButton *btn10 in arr10)
{
CAGradientLayer *btnGradient2 = [CAGradientLayer layer];
btnGradient2.frame = btn10.bounds;
btnGradient2.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:151.0/255.0f green:206.0/255.5 blue:99.0/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:126.0/255.0f green:192.0/255.5 blue:65.0/255.0 alpha:1]CGColor],
nil];
[btn10.layer insertSublayer:btnGradient2 atIndex:0];
}
斯威夫特 3.0
let image = UIImage(named:"NoConnection")!
warningButton = UIButton(type: .system)
warningButton.setImage(image, for: .normal)
warningButton.tintColor = UIColor.lightText
warningButton.frame = CGRect(origin: CGPoint(x:-100,y:0), size: CGSize(width: 59, height: 56))
self.addSubview(warningButton)
如果您在 iOS 15 之后到達這里並且您正在使用新的UIButton.Configuration
API,那么您可能需要通過imageColorTransformer
來完成。
看起來像這樣:
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in .green }
為了方便起見,您可以創建一個擴展:
extension UIButton.Configuration {
func imageColor(_ color: UIColor) -> UIButton.Configuration {
var configuration = self
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in color }
return configuration
}
}
// Usage:
configuration = configuration.imageColor(.green)
與其他答案一樣,圖像本身必須是 Xcode 資產中的“渲染為模板圖像”,或代碼image.withRenderingMode(.alwaysTemplate)
獎金提示:
如果您希望在突出顯示按鈕時更改圖像顏色怎么辦? 然后你的配置擴展看起來像這樣:
func imageColor(whenNormal: UIColor,
whenHighlighted: UIColor,
isHighlighted: Bool) -> UIButton.Configuration {
var configuration = self
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in
isHighlighted ? whenHighlighted : whenNormal
}
return configuration
}
這本身必須從configurationUpdateHandler
上下文中調用,如下所示:
someButton.configurationUpdateHandler = { button in
guard var configuration = button.configuration else { return }
configuration.image = UIImage(named: "some_image")
configuration = configuration.imageColor(whenNormal: .green,
whenHighlighted: .green.withAlphaComponent(0.7),
isHighlighted: button.isHighlighted)
button.configuration = configuration
}
請注意, configurationUpdateHandler
也是您可以根據按鈕狀態實際定義不同圖像的地方。
更改按鈕圖像或圖像視圖色調顏色 Swift :
btn.imageView?.image = btn.imageView?.image?.withRenderingMode(.alwaysTemplate)
btn.imageView?.tintColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
以上都不適合我,因為點擊后色調被清除。 我不得不使用
button.setImageTintColor(Palette.darkGray(), for: UIControlState())
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red
如果您通過UIColor(r:g:b:alpha:)
設置UIButton.tintColor
,請記住將值除以255
。 這些 RGB 值應該在 0 和 1 之間。
我在突出顯示的 state 中遇到了屏蔽圖像的問題。我不希望它發生。 如果您遇到同樣的問題,請查看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.