开发者

This objective C code does not change the color of label text

Debugger show some value in color . Please what am I doing wrong

@synthesize textLabel;
@synthesize textField;
@synthesize sliderRed;
@synthesize sliderGreen;
@synthesize sliderBlue;


- (void)dealloc
{
    [textLabel release];
    [super dealloc];
}

- (IBAction)updateLabel
{
    NSString * textValue = [textField text];

    float red = [sliderRed value]/255.f;
    float green = [sliderGreen value]/255.f;
    float blue = [sliderBlue value]/255.f;

    UIColor *textColour = [[UIColor alloc]initWithRed:red green开发者_运维知识库:green blue:blue alpha:1.0];
    [textLabel setText:textValue];
    [textLabel setTextColor:textColour];
}


I guess sliderRed, sliderGreen and sliderBlue are UISlider instances? What are their min/max values? If you left it at default 0.0 to 1.0 then this code would give you some really low values:

float red = [sliderRed value]/255.f;
float green = [sliderGreen value]/255.f;
float blue = [sliderBlue value]/255.f;

UIColor's method you use takes float parameters from 0.0 to 1.0 so simply passing it the slider values without dividing them would work.

And don't forget to put [textColour release]; at the end of that method or you will be leaking a new UIColor instance every time the method gets called.


Nothing wrong on your codes. maybe something wrong in other places.
Try to replace this line:
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
to
UIColor *textColour = [UIColor redColor];

And check if the redColor works. if not, so it might something wrong in your other codes.


Try adding [textLabel setNeeedsDisplay] after you've changed the drawing attributes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜