1

我有要求UILabel在两种颜色之间显示背景拆分,如下图所示:

在此处输入图像描述

(这里的颜色底部是黑色,顶部是 50% 灰色 - 但这并不重要)。我尝试在界面生成器中将标签的背景颜色设置为 50% 灰色,然后在代码中执行此操作:

CALayer *sl1 = [[[CALayer alloc] init] autorelease];
sl1.frame = CGRectMake(0, lbl.frame.size.height / 2, lbl.frame.size.width, score1.frame.size.height/2);
sl1.backgroundColor = [[UIColor blackColor] CGColor];
[lbl.layer insertSublayer:sl1 atIndex:0];

不幸的是,这导致黑色部分被绘制在文本上,因此标签如下所示:

在此处输入图像描述

不用说,这不是我需要的。那么如何在不使用自定义图像的情况下获得这个背景呢?问题是我需要UILabel在几个不同大小的地方都有这样的 - 所以我需要创建多个版本的背景图像。

有任何想法吗?谢谢。

4

3 回答 3

2

这有效:

UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
myLabel.text = @"Ciao";
myLabel.textColor = [UIColor greenColor];

UIGraphicsBeginImageContext(CGSizeMake(100, 50));   
CGContextRef context = UIGraphicsGetCurrentContext();
// drawing with a gray fill color
CGContextSetRGBFillColor(context,  0.4, 0.4, 0.4, 1.0);
// Add Filled Rectangle, 
CGContextFillRect(context, CGRectMake(0.0, 0.0, 100, 50));

// drawing with a black fill color
CGContextSetRGBFillColor(context,  0., 0., 0., .9);
// Add Filled Rectangle, 
CGContextFillRect(context, CGRectMake(0.0, 25, 100, 25));

UIImage* resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

myLabel.backgroundColor = [UIColor colorWithPatternImage:resultingImage];
[self.view addSubview:myLabel];
于 2012-05-28T10:36:30.063 回答
1

使用. UIColor_ +colorWithPatternImage:传入 1pxUILabel的高度图像,它将在视图的宽度上“平铺”。

myLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"label-background.png"]];
于 2012-05-28T10:18:20.670 回答
0

首先,您必须对渐变背景进行子类化UILabel并覆盖它的drawRect:方法

- (void)drawRect:(CGRect)rect 
{
    //////////////GET REFERENCE TO CURRENT GRAPHICS CONTEXT
    CGContextRef context = UIGraphicsGetCurrentContext();

    //////////////CREATE BASE SHAPE WITH ROUNDED CORNERS FROM BOUNDS
CGRect activeBounds = self.bounds;
CGFloat cornerRadius = 10.0f;   
CGFloat inset = 6.5f;   
CGFloat originX = activeBounds.origin.x + inset;
CGFloat originY = activeBounds.origin.y + inset;
CGFloat width = activeBounds.size.width - (inset*2.0f);
CGFloat height = activeBounds.size.height - (inset*2.0f);

CGRect bPathFrame = CGRectMake(originX, originY, width, height);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:bPathFrame cornerRadius:cornerRadius].CGPath;

//////////////CREATE BASE SHAPE WITH FILL AND SHADOW
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f].CGColor);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 6.0f, [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor);
CGContextDrawPath(context, kCGPathFill);

//////////////CLIP STATE
CGContextSaveGState(context); //Save Context State Before Clipping To "path"
CGContextAddPath(context, path);
CGContextClip(context);

//////////////DRAW GRADIENT
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t count = 3;
CGFloat locations[3] = {0.0f, 0.57f, 1.0f}; 
CGFloat components[12] = 
{   0.0f/255.0f, 0.0f/255.0f, 0.0f/255.0f, 1.0f,     //1
    5.0f/255.0f, 5.0f/255.0f, 5.0f/255.0f, 1.0f,     //2
    10.0f/255.0f, 10.0f/255.0f, 10.0f/255.0f, 1.0f};    //3
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);

CGPoint startPoint = CGPointMake(activeBounds.size.width * 0.5f, 0.0f);
CGPoint endPoint = CGPointMake(activeBounds.size.width * 0.5f, activeBounds.size.height);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}

这将绘制黑色到白色的渐变背景可能这将帮助你快乐编码:) 上面的代码来自这个网站http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

于 2012-05-28T10:20:37.267 回答