2013-02-25 24 views
6

我想创建一个放在另一个图像上的文本图像。文字需要有白色填充和黑色笔画轮廓。我使用的是Objective-C,它适用于iPhone。我可以让文字出现;然而,我不能缝合以找出如何获得笔画并填充颜色来改变。如何在iPhone的UIGraphicsImageContext中使用属性(特别是笔画和填充颜色)绘制文本

self.bigImage是一个指向我xib中的UIImage的指针。

这里是我的代码:(此代码的工作......我希望它有一个黑色的中风但是白色填充)

- (IBAction)submitBtn:(id)sender { 

    UIFont *myfont = [UIFont fontWithName:@"myfont" size:32]; 

    UIImage *textImage = [self imageFromText:@"teststring" font:myfont]; 
    CGSize finalSize = [self.bigImage.image size]; 
    CGSize textSize = [textImage size]; 

    UIGraphicsBeginImageContext(finalSize); 
    [self.bigImage.image drawInRect:CGRectMake(0,0, finalSize.width, finalSize.height)]; 
    [textImage drawInRect:CGRectMake(0, 0, textSize.width, textSize.height)]; 

    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    self.bigImage.image = newImg; 
} 

-(UIImage *)imageFromText:(NSString *)text font:(UIFont *)font { 
    CGSize size = [text sizeWithFont:font]; 

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
    if (UIGraphicsBeginImageContextWithOptions != NULL) 
     UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    else 
     // iOS is < 4.0 
     UIGraphicsBeginImageContext(size); 

    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *Image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return Image; 
} 



我可以得到它来呈现一个或另一个。如果我添加此代码,我得到一个黑色的行程:

CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 



如果我添加此代码,我得到一个白色字体

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 

,但如果我同时添加snipets我得到的黑色中风但字体颜色是透明...



解决:得益于“布朗康”一点帮助:

我需要drawAtPoint之前添加的这段代码:

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 

回答

3

我通过修改布朗康的解决了这个问题

[[UIColor blackColor] set]; 

之前post ...我需要在drawAtPoint之前添加snipet代码...

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 
3

两个问题需要解决。

首先,为了绘制背景,不能依赖drawAtPoint:withFont:,因为它不绘制背景。

使用CGContextFillRect绘制背景色:

[[UIColor whiteColor] set]; 
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height) 

其次,您需要使用描边色设置呼叫[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

+0

我不是在寻找使背景颜色为白色,但字体本身白加黑中风。我希望背景的颜色透明。 – werdsackjon 2013-02-25 20:06:13

8

我固定的:

[theText drawAtPoint:CGPointMake(x, y) 
        withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }]; 

终于!

感谢列维

Levi

相关问题