开发者

Retrieving default UI colors on iPhone

PREAMBLE: since iOS 13 with its dark theme, there's a proper notion of system colors on iOS. See the UICol开发者_如何学编程or class.


Here's my iPhone project with a XIB file with a regular UIButton on it. The button has a text color - a nice shade of blue, RGB(50, 79, 133) to be precise.

Is there a quick and proper way to retrieve that color value in runtime? Not the color of this particular button, but the default text color of UIButtons? Preferably as a UIColor * or a CGColorRef.

Is there a notion of system colors on iPhone, or this is just a default enforced by the presets in the Interface Builder?

Yes, I can do some elementary math and convert the RGB into the floating-point (UGH!) representation that UIColor uses. Anything more elegant than that?


For the UIColor problem may I suggest adding a category to UIColor, like this:

UIColor+Extras.h

#import <UIKit/UIKit.h>

@interface UIColor(Extras)
+ (UIColor *)colorWithRGBA:(NSUInteger) rgba;
+ (UIColor *)colorWithRGB:(NSUInteger) rgb;
@end

UIColor+Extras.m

#import "UIColor+Extras.h"

@implementation UIColor(Extras)
+ (UIColor *)colorWithRGBA:(NSUInteger) rgba
{
    return [UIColor colorWithRed:(rgba >> 24) / 255.0f green:(0xff & ( rgba >> 16)) / 255.0f blue:(0xff & ( rgba >> 8)) / 255.0f alpha:(0xff & rgba) / 255.0f];
}

+ (UIColor *)colorWithRGB:(NSUInteger) rgb
{
    return [UIColor colorWithRed:(rgb >> 16) / 255.0f green:(0xff & ( rgb >> 8)) / 255.0f blue:(0xff & rgb) / 255.0f alpha:1.0];
}
@end

This will let you write stuff like [UIColor colorWithRGBA:0xFF00FFFF] or [UIColor colorWithRGB:0xFF00FF]

As fo the starting color values, some of them are documented in the header files, like UIButton.h, and there are also some colors available as class methods on UIColor, like [UIColor lightTextColor] [UIColor darkTextColor] etc. These are documented in the UIColor class reference.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIColor_Class/Reference/Reference.html

If you want to retrieve the default color there is always:

[[button titleLabel] textColor]

To get the text color of any button. If you want the default color, create a hidden button and check the text color of it. Not very elegant mind you.


If you want the default button text color, all you have to do is:

label.textColor = [button titleColorForState: UIControlStateNormal];

You can get the other colors by specifying a different UIControlState.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜