代码之家  ›  专栏  ›  技术社区  ›  Adam

NSString drawInRect与所有字符重叠

  •  2
  • Adam  · 技术社区  · 15 年前

    我试图用渐变填充填充一些文本,通过设置文本绘制模式为剪裁,然后绘制渐变填充。

    我的代码如下所示:

    CGRect r = CGRectInset(self.frame, 55, 8);      
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGFloat components[8] = {44/255, 54/255, 66/255, 1.0
                ,75/255, 92/255, 111/255, 1.0};
    CGFloat locations[2] = {0, 1};
    
    // draw the text's gradient fill
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
    
    CGContextSetTextDrawingMode(context, kCGTextClip);
    
    
    [monthString drawInRect:r withFont:f lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    
    CGContextFillRect(context, CGRectMake(0, 0, 320, 20));
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Community CDub    8 年前

    使用UIKit NSString添加的drawInRect:withFont:等工具在kCGTextClip绘图模式下似乎效果不佳。我见过同样的行为,而且 this answer 我提供了使用纯石英绘图调用用渐变填充文本的代码。这种方法的缺点是您仅限于MacRoman文本编码,它缺乏对许多Unicode符号的支持。