kCGTextStroke的Fill和Stroke位置不正确 [英] kCGTextStroke's Fill and Stroke aren't positioned correctly

查看:234
本文介绍了kCGTextStroke的Fill和Stroke位置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用下面的代码对 UILabel 中的文本应用笔触(并填充),结果如下图所示。笔划的一侧比另一侧重(与字母的底部相比,笔画的底部较重,而与左侧的字母相比,笔画则较右。末尾的句号也使它看起来非常像一只呆呆的眼睛)。我根本没有打开任何阴影,所以我不认为这是阴影对笔画的干扰。

So I'm using the code below to apply a stroke (and fill) to text in a UILabel, and it's coming out like the image below. The stroke is heavier on one side than the other (look at the top of the letters compared to the bottom, and the right compared to the left. The period at the end makes it very noticeable, too, looks like a googly eye). I've not got any shadowing turned on at all, so I don't think it's the shadow interfering with the stroke.

是什么原因造成的?

- (void) drawTextInRect: (CGRect) rect
{
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetTextDrawingMode(c, kCGTextFillStroke);
    CGContextSaveGState(c);
    CGContextSetRGBFillColor(c, 1.0, 0.0, 0.0, 1.0);
    CGContextSetRGBStrokeColor(c, 0.0, 1.0, 0.0, 1.0);
    [super drawTextInRect: rect];
    CGContextRestoreGState(c);
}

编辑:因此,我踢了一下标签只有填充,只有笔触。取消笔触会产生一段看起来完全正常的文本,就像我刚刚在Interface Builder中将其上色一样。但是,关闭填充仅显示笔触,该笔触在任何一侧看上去都比其他任何地方都重。 **这使我相信问题是填充相对于笔画位于位置,并且填充或笔画本身都不存在错误。还有其他想法吗?如何使填充直接位于笔画的中心?

So, for kicks, I took at look at the label with only the fill, and only the stroke. Turning off the stroke creates a perfectly normal-looking piece of text, as if I'd just coloured it in Interface Builder. Turning off the fill, however, shows only the stroke, which doesn't look heavier on any side than any other. **This leads me to believe that the issue is where the fill is positioned in relation to the stroke, and that neither the fill or stroke themselves are at fault. Any other thoughts on this? How can I get the fill directly centred in the stroke?

推荐答案

您可能应该只使用 kCGTextFillStroke 作为绘制模式,只绘制一次(设置了单独的笔触和填充颜色)。

You should probably just use kCGTextFillStroke as the drawing mode and only draw once (with separate stroke and fill colors set).

CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);   // any color you want (this is red)
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0); // any color you want (this is green)
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
[self.text drawInRect:rect withFont:self.font];

或者,您之后也可以中风。笔划通常从中心绘制,这意味着宽度的一半向内,一半向外。这意味着如果您在笔触之后进行填充,那么某些笔触将被填充所覆盖。

Alternatively you could just stroke afterwards. Strokes are usually drawn from the center which means that half of the width is inwards and half is outwards. That would mean that if you fill after you stroke some of the stroke is going to get covered up by the fill.

这篇关于kCGTextStroke的Fill和Stroke位置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆