xcode 如何在 iPhone 的 UIGraphicsImageContext 中使用属性(特别是描边和填充颜色)绘制文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15074412/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:51:09  来源:igfitidea点击:

How to draw text with attributes (Specifically Stroke and Fill color) in UIGraphicsImageContext for iPhone

iphoneiosobjective-cxcodeimage

提问by werdsackjon

I am trying to create an image of text to put on top of another image. The text needs to have a white fill and a black stroke outline. I am using objective-C and it is for the iPhone. I can get the text to show up; however, I cannot seam to figure out how to get the stroke and fill color to change.

我正在尝试创建一个文本图像以放置在另一个图像之上。文本需要有白色填充和黑色描边轮廓。我正在使用objective-C,它适用于iPhone。我可以让文字显示出来;但是,我无法弄清楚如何改变笔触和填充颜色。

self.bigImageis a pointer to a UIImage in my xib.

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

Here is my code: (This code works... I want it to have a black stroke with white fill however)

这是我的代码:(此代码有效......但是我希望它有一个带有白色填充的黑色描边)

- (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;
}



I can get it to render one or the other. If I add this code I get a black stroke:



我可以让它渲染一个或另一个。如果我添加此代码,我会得到一个黑色的笔划:

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



If I add this code I get a white font:



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

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

But If I add both snipets I get the black stroke but the font color is transparent...

但是,如果我添加两个 snipets,我会得到黑色笔划,但字体颜色是透明的...



Solved:thanks to a little help from 'Brane':



已解决:感谢“Brane”的一点帮助:

I needed to add this bit of code before the drawAtPoint:

我需要在以下代码之前添加这段代码drawAtPoint

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

采纳答案by werdsackjon

I fixed this by modifying Brane's post... I needed to add this snipet of code before drawAtPoint...

我通过修改 Brane 的帖子修复了这个问题......我需要在drawAtPoint......之前添加这段代码......

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

回答by Nicola

I fixed with:

我修复了:

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

finally!!!

最后!!!

thanks to levi

感谢列维

Levi

列维

回答by Brane

Two problems to solve.

两个问题要解决。

First, in order to draw the background, you can't rely on drawAtPoint:withFont:as it does not draw background.

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

Use CGContextFillRect to draw your background color:

使用 CGContextFillRect 绘制背景颜色:

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

Second, you need to set the stroke colour using

其次,您需要使用设置笔触颜色

[[UIColor blackColor] set];

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

在打电话之前 [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];