2010-03-24 101 views
-1

我的代碼是如何以編程方式更改文本的顏色?

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1 
{ 
    int w = img.size.width; 
    int h = img.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); 
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1); 

    char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding]; 
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 

    CGContextSetRGBFillColor(context, 255, 255, 255, 2); 

    CGContextShowTextAtPoint(context, 10, 170, text, strlen(text)); 

    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    return [UIImage imageWithCGImage:imageMasked]; 
} 

我們如何才能改變編程文本的顏色? 答案將不勝感激!

回答

1

我的猜測是將CGContextSetRGBFillColor的最後一個實例更改爲您想要的RGB值。現在它是白色的。將255,255,255部分更改爲您想要的對應RGB值(每個顏色通道0.0 - 1.0個範圍)。即。紅色將是1.0,0.0,0.0;藍0.0,0.0,1.0,黃1.0,1.0,0;黑色0.0,0.0,0.0;等....祝你好運

編輯:範圍是0.0-1.0不0-255

+0

這不是'CGContextSetRGBFillColor'函數的參數範圍。 – 2010-03-24 15:27:52

+0

你說得對,0-1.0。我會編輯它。 – Adolfo 2010-03-25 15:53:16

+0

非常感謝!現在我懂了。 – isarathg 2010-03-26 08:05:41

1
CGContextSetRGBFillColor(context, 255, 255, 255, 2); 

我們怎樣才能改變編程的文本的顏色?

這是一種方式,雖然你做錯了。見the documentation;所有這四個值都超出範圍,其中三個是方式超出範圍。

+0

非常感謝!現在我懂了。 – isarathg 2010-03-26 08:05:24

相關問題