【问题标题】:Drawing Text on UILabel with a gradient image使用渐变图像在 UILabel 上绘制文本
【发布时间】:2013-07-08 14:48:06
【问题描述】:

我有一个 UILabel 子类,它使用图案图像在 drawinRect 方法中绘制一些文本。此图案图像是我正在创建的渐变图像。

-(void)drawRect:(CGRect)rect
{
      UIColor *color = [UIColor colorWithPatternImage:[self gradientImage]];
      [color set];

      [someText drawInRect:rec withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:someAlignment];
}

梯度图像法是

-(UIImage *)gradientImage
{
    NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
    UIColor *co = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

    colors = [NSArray arrayWithObjects:co,co,[UIColor whiteColor],co,co,co,co,nil];

    NSArray *gradientColors=colors;

    CGFloat width;         
    CGFloat height;      

    CGSize textSize = [someText sizeWithFont:font];

    width=textSize.width; height=textSize.height;

    UIGraphicsBeginImageContext(CGSizeMake(width, height));

    // get context
    CGContextRef context = UIGraphicsGetCurrentContext();

    // push context to make it current (need to do this manually because we are not drawing in a UIView)
    UIGraphicsPushContext(context);

    //draw gradient
    CGGradientRef gradient;
    CGColorSpaceRef rgbColorspace;

    //set uniform distribution of color locations
    size_t num_locations = [gradientColors count];

    CGFloat locations[num_locations];
    for (int k=0; k<num_locations; k++)
    {
        locations[k] = k / (CGFloat)(num_locations - 1); //we need the locations to start at 0.0 and end at 1.0, equaly filling the domain
    }

    //create c array from color array
    CGFloat components[num_locations * 4];
    for (int i=0; i<num_locations; i++)
    {
        UIColor *color = [gradientColors objectAtIndex:i];
        NSAssert(color.canProvideRGBComponents, @"Color components could not be extracted from StyleLabel gradient colors.");
        components[4*i+0] = color.red;
        components[4*i+1] = color.green;
        components[4*i+2] = color.blue;
        components[4*i+3] = color.alpha;
    }

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGRect currentBounds = self.bounds;

    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(context, gradient, topCenter, midCenter, 0);


    CGGradientRelease(gradient);
    CGColorSpaceRelease(rgbColorspace);

    // pop context
    UIGraphicsPopContext();

    // get a UIImage from the image context
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();

    // clean up drawing environment
    UIGraphicsEndImageContext();

    return  gradientImage;
}

一切正常。文字以漂亮的光泽效果绘制。现在我的问题是,如果我更改 UILabel 内文本的 x 或 y 位置,然后调用 [someText drawinRect:rec]... 重绘,光泽效果生成不同。似乎图案图像随着文本位置的变化而变化..

下面是frame的效果rec = CGRectMake(0, 10, self.frame.size.width, self.frame.size.height);

下面是frame的效果rec = CGRectMake(0, 40, self.frame.size.width, self.frame.size.height);

我希望我已经很好地解释了我的问题。在其他地方找不到任何确切的答案。提前致谢。

【问题讨论】:

    标签: ios core-graphics gradient


    【解决方案1】:

    您可能想考虑使用现有的第 3 方解决方案,例如 FXLabel

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多