0

我正在尝试获取 UITextView 使用的颜色。

为此,我写道,

myTextView.textColor

但这给了我输出

UIDeviceRGBColorSpace 0.439216 0.439216 0.439216 1

知道如何获得实际的 RGB 组合吗?


我懂了...

我需要将它乘以 255 :D :P

当 0.439216*255 为 112 时,为什么我的颜色比较没有给出想要的结果?

我应用到 UITextView 的颜色如下使用常量。

#define placeColor [UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]

当我比较为

if (aboutCompany.textColor == placeColor) {
    NSLog(@"place color..");
    aboutCompany.text = @"";
    aboutCompany.textColor = fottColor;
} else {
    NSLog(@"not place color..");
}

我得到,而不是地方颜色......

有什么理由WHY?

4

1 回答 1

0

当您比较两个 NSObject(或 NSObject 子类,在这种情况下)时,您不想使用 == 运算符。== 运算符不会告诉您两个对象是否具有相同的值,它只会告诉您是否正在比较同一个对象。您需要使用 'compare' 或 'isEqual' 方法。(Compare 有一些额外的开销,因为它实际上返回一个枚举,指示三种状态之一,分别对应于 <、= 和 >。)

“在幕后”,正在发生的事情是,在 Objective-C 中,您永远不会直接声明 NSObject,您只会声明一个指针,然后用对象填充的地址空间填充指针。

{
    NSObject *foo=[[Foo alloc] init];
    NSObject *bar=foo;

    foo==bar;//true
    //If you inspect foo and bar really closely in the debugger, you'll discover they're both just integers pointing to a 'random' memory space.
}

{
    NSNumber *alpha=@(1);
    NSNumber *beta=@(1);
    //Again, if you inspect these very closely, you'll discover they're pointers to memory space, but this time you're pointing to two different spaces in memory, even though you're storing the same data in them.
    alpha==beta;//False, they're two separate objects
    [alpha isEqualTo:beta];//true; their value is identical
    [alpha intValue]==[beta intValue;//Again, true because their value is identical
}

此外,在处理浮点值时,我会非常小心地信任相等运算符的结果——并且 UIColor 是基于浮点数构建的。浮动有一个不相等的讨厌习惯,即使他们“应该”是。它与数字的底层机器表示有关,它可能在数字中有一定程度的“噪音”。大多数时候,这种噪音相对来说是微不足道的(例如 +/- 1*10^-10),但是当您想要进行相等运算时,它会变得非常痛苦。网上有很多文章可以更好地介绍这一点,请随意查找它们,但是您需要检查两个数字之间的差异是否小于给定的 epsilon——差异小于 10,而不是相等^-5,例如。究竟如何推导合适的 epsilon 是我无法帮助您的,您需要自己进行研究。如果可以的话,我只是避免使用浮点数来避免头痛,当然也不会做任何需要对它们使用相等运算符的事情。

于 2013-10-02T14:31:00.817 回答