NSString drawAtPoint: 和 kCGTextFillClip,为什么不起作用?
我面临 NSString 方法 drawAtPoint 和 kCGTextFillClip 的问题。我需要创建一个带有渐变而不是简单颜色的 UILabel,因此我对其进行了子类化并覆盖了 drawRect 方法。我设法通过使用函数 CGContextShowTextAtPoint 获得我想要的东西,但它不能正确处理 UTF-8,这对我来说至关重要。
我知道这是一个常见问题,所以经过一番搜索后,我发现我可以使用drawAtPoint来解决这个编码问题。事实上,文本现在可以正确显示。问题是我不知道如何让 kCGTextFillClip 继续工作。
查看结果。
正如您所看到的,它似乎可以很好地剪辑到第一个字母,但之后就不行了。您知道如何解决这个问题吗?
这是我正在使用的代码:
- (void)drawRect:(CGRect)rect {
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGRect viewBounds = self.bounds;
CGContextSaveGState(theContext);
CGContextTranslateCTM(theContext, 0.0, viewBounds.size.height/2 + 1);
CGContextSetTextDrawingMode(theContext, kCGTextFillClip);
[self.text drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:11.5]];
// Creation of the gradient
CGFloat locations[2] = { 0.0f, 1.0f };
CGFloat components[8] = {
190.0/255, 190.0/255, 190.0/255, 1.0, // Start color
113.0/255, 113.0/255, 113.0/255, 1.0 // End color
};
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, 2);
CGPoint topCenter = CGPointMake(CGRectGetMidX(viewBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds));
CGContextDrawLinearGradient(theContext, gradient, topCenter, midCenter, 0);
谢谢!
I am facing a problem with NSString method drawAtPoint and kCGTextFillClip. I needed to create a UILabel with a gradient instead of a simple color, so I've subclassed it and overrode the drawRect method. I managed to get what I wanted by using the function CGContextShowTextAtPoint, but it doesn't handle UTF-8 properly, which is essential to me.
I am aware that this is a common problem so after searching for a bit, I found out that I could use drawAtPoint to solve this encoding problem. Indeed, the text is now showing properly. The problem is I don't know how to make kCGTextFillClip work anymore.
Look at the result.
As you can see, it seems to clip to the first letter just fine, but not after that. Do you have any idea on how to solve this issue?
Here's the code I'm using :
- (void)drawRect:(CGRect)rect {
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGRect viewBounds = self.bounds;
CGContextSaveGState(theContext);
CGContextTranslateCTM(theContext, 0.0, viewBounds.size.height/2 + 1);
CGContextSetTextDrawingMode(theContext, kCGTextFillClip);
[self.text drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:11.5]];
// Creation of the gradient
CGFloat locations[2] = { 0.0f, 1.0f };
CGFloat components[8] = {
190.0/255, 190.0/255, 190.0/255, 1.0, // Start color
113.0/255, 113.0/255, 113.0/255, 1.0 // End color
};
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, 2);
CGPoint topCenter = CGPointMake(CGRectGetMidX(viewBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds));
CGContextDrawLinearGradient(theContext, gradient, topCenter, midCenter, 0);
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试着一个字母一个字母地画?
Try and draw letter by letter?