xcode 如何更改iphone sdk中标签的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6908371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change colors of a label in iphone sdk?
提问by sairam
i am using one for loop for creating labels those are having text stored in nsarray.I want to display 1st labeltext color red and 2nd labeltext white and 3rd red ,4th white,5th red and 6th white ..........etc for all the labels that are stored in an array how to do this.help me on that ..can anyone shathanks in advance
我正在使用一个 for 循环来创建标签,这些标签的文本存储在 nsarray 中。我想显示第一个标签文本颜色红色和第二个标签文本白色和第三个红色、第 4 个白色、第 5 个红色和第 6 个白色...... .etc 对于存储在数组中的所有标签如何执行此操作。帮我解决这个问题 .. 任何人都可以提前 shathanks
回答by Heiberg
If you want to alternate between two colors the simplest thing is to use the modulo operator (%) to figure out if you are on an even or odd label index.
如果您想在两种颜色之间交替,最简单的方法是使用模运算符 (%) 来确定您是在偶数还是奇数标签索引上。
Here is one way of doing this:
这是执行此操作的一种方法:
int labelIndex = 0;
for (NSString *labelText in labelArray)
{
UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = labelText;
label.textColor = (labelIndex % 2) ? [UIColor whiteColor] : [UIColor redColor];
// Do something with the label here.
labelIndex++;
}
You can also use this technique to alternate between more than two different colors, but then I would suggest that you put the colors into an array of their own and index that array using (labelIndex % numColors) or something similar.
您也可以使用这种技术在两种以上不同的颜色之间交替,但我建议您将颜色放入它们自己的数组中,并使用 (labelIndex % numColors) 或类似的方法索引该数组。
回答by Praveen S
After you create a uilabel you can set the background color:
创建 uilabel 后,您可以设置背景颜色:
myLabel.backgroundcolor = [UIColor clearcolor]; // Specify what you want the
background color to be.
myLabel.textColor = [UIColor whiteColor]; // specify the text color for this property.
回答by Parth Bhatt
Give tags to labels starting from 1 and then just use this method.
从 1 开始为标签添加标签,然后使用此方法。
for(int i=1;i<=[array count];i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i;
[label setFrame:<your-frame>];
if(label.tag % 2 == 1)
{
[label setBackgoundColor:[UIColor blackColor]]; // This line may be optional for you
[label setTextColor:[UIColor redColor]];
}
else if(label.tag % 2 == 0)
{
[label setBackgoundColor:[UIColor blackColor]]; // This line may be optional for you
[label setTextColor:[UIColor whiteColor]];
}
}
Hope this helps you.
希望这对你有帮助。
回答by Rahul Vyas
you can store UIColor objects in array and set on label while you set text from array in your loop. like this
您可以将 UIColor 对象存储在数组中并在标签上设置,同时在循环中设置数组中的文本。像这样
label.textColor = [ColorsArray objectAtIndex:loopConuter];
回答by Lewen
You mean that you want to set colors on an array that is not constant sized? maybe you can accomplish it with
您的意思是要在大小不固定的数组上设置颜色?也许你可以用
[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>]
it takes 4 float values from 0 to 1, maybe you don't need the transparency. then you could write a function, which takes 1 int value and outputs 3 float values. if you don't really want to have the colors set in a definite order, maybe you can generate 3 random numbers and than transform them into [0,1]
它需要从 0 到 1 的 4 个浮点值,也许您不需要透明度。那么你可以编写一个函数,它接受 1 个 int 值并输出 3 个浮点值。如果您真的不想按确定的顺序设置颜色,也许您可以生成 3 个随机数,然后将它们转换为 [0,1]