有一种简单的方法可以转换UIColor为十六进制值吗?
或者我们是否必须使用RGB组件,CGColorGetComponents然后从那里开始工作?
例如CGColorGetComponents(color.CGColor)[0] * 256?
我试图比较颜色,但我不能使用该isEqual方法,因为我试图比较一个背景的颜色UICollectionViewCell.
在这种情况下比较颜色的正确方法是什么?
if(cell!.layer.backgroundColor! == UIColor.redColor().CGColor)
{
cell?.layer.backgroundColor = UIColor.redColor().CGColor
}
Run Code Online (Sandbox Code Playgroud) 现在我真的很困惑.下面是变量如何实例化:
Utils.redColor = UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
Run Code Online (Sandbox Code Playgroud)
在这里,我枚举一个属性文本的属性,以跳过颜色,如果它等于Utils.redColor:
text.enumerateAttributes(in: NSRange(0..<text.length), options: []) { (attributes, range, _) -> Void in
for (attribute, object) in attributes {
if let textColor = object as? UIColor{
NSLog("textColor = \(textColor) red = \(Utils.redColor!)")
if (!textColor.isEqual(Utils.redColor!)){
//I need to repaint any textColor other than red
text.setAttributes(textAttributes , range: range)
}
}
Run Code Online (Sandbox Code Playgroud)
所以,正如你在这段代码中看到的,textColor 也是一个UIColor对象,但是日志说:
textColor = kCGColorSpaceModelRGB 0.666667 0.172549 0.172549 1 red = UIExtendedSRGBColorSpace 0.666667 0.172549 0.172549 1 …Run Code Online (Sandbox Code Playgroud) 我想改变不同状态的按钮背景.我试试这样:
@IBAction func addToShedulerAction(sender: UIButton) {
println(sender.backgroundColor)
if sender.backgroundColor==UIColor.redColor(){
sender.backgroundColor==UIColor.whiteColor()
}
else //if sender.backgroundColor==UIColor.whiteColor()
{
sender.backgroundColor=UIColor.redColor()
}
}
Run Code Online (Sandbox Code Playgroud)
但在第一个按钮println打印nil和背景变为红色,在第二次打印println打印"可选(UIDeviceRGBColorSpace 1 0 0 1)"和颜色不会改变
我需要点击一个UIImageView...
...并将点击位置的颜色与从资产目录返回的颜色(或在代码中创建的自定义颜色)进行比较。
我在色彩空间方面遇到了很多麻烦,我的比赛总是没有。我已经阅读了一些关于 stackoverflow 的很棒的例子并尝试了它们,但我一定还是做错了什么。
我还尝试使用这样的自定义颜色(而不是资产颜色):
+ (UIColor *)themeRed {
return [UIColor colorWithRed:192.0f/255.0f green:92.0f/255.0f blue:42.0f/255.0f alpha:1];
}
Run Code Online (Sandbox Code Playgroud)
仍然没有匹配项。我的测试匹配代码如下:
-(void)tappedColorView:(UITapGestureRecognizer *)tapRecognizer {
CGPoint touchPoint = [tapRecognizer locationInView: uiiv_hs];
//NSLog(@"my color %@", [uiiv_hs colorOfPoint:touchPoint]);
UIColor *color = [uiiv_hs colorOfPoint:touchPoint];
NSLog(@"color %@",[uiiv_hs colorOfPoint:touchPoint]);
UIColor *matchcolor = [UIColor themeRed];
NSLog(@"mcolor %@",[UIColor themeRed]);
NSArray *colors = [NSArray arrayWithObjects:[UIColor colorNamed:@"Color01"],[UIColor colorNamed:@"Color02"], nil];
if ([color matchesColor:matchcolor error:nil]) {
NSLog(@"1Match!");
} else {
NSLog(@"1No Match!");
}
if ([color isEqualToColor:[UIColor …Run Code Online (Sandbox Code Playgroud) 这不起作用:
if([myView.backgroundColor isEqual:[UIColor clearColor]])
NSLog("Background is clear");
else
NSLog("Background is not clear");
Run Code Online (Sandbox Code Playgroud)
PS:要重现这种情况,请在界面生成器中拖动 uiview,将其背景颜色设置为界面生成器中的清除颜色。设置视图的出口,然后使用上面的代码在 viewDidLoad 中进行比较。
这是测试项目的链接:https://drive.google.com/file/d/0B_1hGRxJtrLjMzUyRHZyeV9SYzQ/view ?usp=sharing
使用Xcode V7.2。尝试进行单元测试时,需要验证是否设置了正确的颜色,并得到以下消息:
Cannot invoke 'XCTAssertEqual' with an argument list of type '(CGColor, CGColor)'
Run Code Online (Sandbox Code Playgroud)
我如何断言CGColor应该是什么?
我想以编程方式知道,我的TextView具有默认的backgroundColor或更改.例如:
if (myTextView.backgroundColor == defaultColor){
NSLog(@"default");
} else {
NSLog(@"changed");
}
Run Code Online (Sandbox Code Playgroud)
我有一个想法:
UITextView *etalon = [UITextVew new];
if (myTextView.backgroundColor == etalon.backgroundColor){
NSLog(@"default");
} else {
NSLog(@"changed");
}
Run Code Online (Sandbox Code Playgroud)
但我认为这不太对.有谁有更好的想法?
ios ×7
swift ×4
uicolor ×4
objective-c ×3
cgcolorspace ×1
swift3 ×1
uikit ×1
uiview ×1
unit-testing ×1
xcode ×1