Ios 使用渐变图像在UILabel上绘制文本
我有一个UILabel子类,它使用模式图像在drawinRect方法中绘制一些文本。这个图案图像是我正在创建的渐变图像Ios 使用渐变图像在UILabel上绘制文本,ios,core-graphics,gradient,Ios,Core Graphics,Gradient,我有一个UILabel子类,它使用模式图像在drawinRect方法中绘制一些文本。这个图案图像是我正在创建的渐变图像 -(void)drawRect:(CGRect)rect { UIColor *color = [UIColor colorWithPatternImage:[self gradientImage]]; [color set]; [someText drawInRect:rec withFont:font lineBreakMode:UIL
-(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;
}
-(UIImage*)梯度图像
{
NSData*colorData=[[NSUserDefaults standardUserDefaults]objectForKey:@“myColor”];
UIColor*co=[NSKeyedUnarchiver unarchiveObjectWithData:colorData];
colors=[NSArray arrayWithObjects:co,co,[UIColor whiteColor],co,co,co,co,nil];
NSArray*渐变颜色=颜色;
CGFloat宽度;
浮子高度;
CGSize textSize=[someText sizeWithFont:font];
宽度=textSize.width;高度=textSize.height;
UIGraphicsBeginImageContext(CGSizeMake(宽度、高度));
//获取上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//推送上下文使其成为当前上下文(需要手动执行此操作,因为我们不是在UIView中绘制)
UIGraphicsPushContext(context);
//绘制坡度
梯度梯度;
CGColorSpaceRef rgbColorspace;
//设置颜色位置的均匀分布
大小\u t数量\u位置=[GradientColor计数];
CGFloat位置[num_位置];
对于(int k=0;k,您可能希望使用现有的第三方解决方案,如