kCGTextStroke's Fill and Stroke aren't positioned correctly

让人想犯罪 __ 提交于 2019-12-06 09:23:06

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.

A possibility is that the overridden method translates the CTM using CGContextTranslateCTM or similar functions. The CTM is part of the state of a context and specifies a transform for all following draw calls.

You should try to save the context before the call to the overridden method and restore it afterwards:

CGContextSaveGState(c);
[super drawTextInRect: rect];
CGContextRestoreGState(c);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!