开发者

Obj-C, set color of UILabel based on plist value?

I have created an iPhon开发者_StackOverflow社区e app with a table that pushes a standard detail view .xib when a row is selected. I've successfully been able to populate the text values of several UILabels I have on that nib file from a plist I have, which is an array of dictionaries of strings. So for each table row selected, the detail view will populate those labels with the information contained within a single dictionary, as the UILabels are linked to the keys in the plist. I was wondering if it would be possible to change the color of each individual UILabel based on a key in each dictionary within that plist.

FOR EXAMPLE: Table row 1 pushes a view that has UILabel 1 in red, UILabel 2 in orange, UILabel 3 in yellow, and so on. Table row 2 pushes a view that has UILabel 1 in purple, UILabel 2 in brown, UILabel 3 in blue, and so on.

I'd like to be able to define each label's color in each dictionary.

There's got to be a way to do this! Thank you so much for your help!


Yes you can do that. You need to define a dictionary of this structure (that's just one way of doing it):

Root Dictionary

     -UILabelA
          Red   -> 143
          Green -> 225
          Red   -> 002

     -UILabelB
          Red   -> 243
          Green -> 235
          Red   -> 235

     -UILabelc
          Red   -> 198
          Green -> 025
          Red   -> 111

Then, lets say you need the color of UILabelA. All you do is:

NSDictionary *labelADict = [[NSDictionary dictionary] objectForKey:@"UILabelA"];
//Then create the color
float red = [[labelADict objectForKey:@"Red"] floatValue] / 255.0f;
float green = [[labelADict objectForKey:@"Green"] floatValue] / 255.0f;
float blue = [[labelADict objectForKey:@"Blue"] floatValue] / 255.0f;

UIColor *color = [UIColor colorWithRed:red
                                 green:green 
                                  blue:blue 
                                 alpha:1.0];
[labelA setTextColor:color];

You can do this dynamically for each of the table/cells.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜