dmaclach的答案只适用于容易反转的形状。我的解决方案是一个自定义视图,可以处理任何形状和文本。它需要iOS 4,并且与分辨率无关。
首先,对代码的作用进行图形解释。这里的形状是一个圆。
代码用白色阴影绘制文本。如果不需要,可以进一步重构代码,因为Dropshadow需要以不同的方式进行屏蔽。如果在旧版本的iOS上需要它,则必须替换块并使用(恼人的)cgBitmapContext。
—(uiimage*)blackquareofsize:(cgsize)大小{
uigraphicsbeginimagecontextwithoptions(大小,否,0);
[[uicolor blackcolor]setfill];
cContextFillRect(uigraphicsgetCurrentContext(),cDirectMake(0,0,size.width,size.height));
uiimage*blacksquare=uigraphicsGetImageFromCurrentImageContext();
uigraphicsEndImageContext();
返回黑方;
}
-(cgImageRef)CreateMaskWithSize:(cgSize)大小形状:(void(^)(void))块{
uigraphicsbeginimagecontextwithoptions(大小,否,0);
块();
cgimageref shape=[uigraphicsGetImageFromCurrentImageContext()cgimage];
uigraphicsEndImageContext();
cgImageRef Mask=cgImageMaskCreate(cgImageGetWidth(shape)),
CGImageGetHeight(形状),
CGImageGetBitsPerComponent(形状),
CGImageGetBitsPerPixel(形状),
CGImageGetBytesPerow(形状),
cImageGetDataProvider(shape),空,假);
返回掩模;
}
-(无效)drawrect:(cgrect)rect{
uiFONT*FONT=[uiFONT FONTWITHNAME:@“Helveticaneue-bold”尺寸:40.0F];
cgsize fontsize=[文本大小带字体:字体];
cgimageref mask=[自我创建的maskwithsize:rect.size形状:]^{
[[uicolor blackcolor]setfill];
cContextFillRect(uigraphicsgetCurrentContext(),rect);
[[uicolor whitecolor]setfill];
//此处显示自定义形状
[文本绘制点:cgpointmake((self.bounds.size.width/2)-(font size.width/2),0),字体:字体];
[文本绘制点:cgpointmake((self.bounds.size.width/2)-(font size.width/2),-1)带字体:字体];
};
cImageRef CutoureRef=cImageCreateWithMask([Self BlackQuareOfSize:rect.size].cImage,Mask);
CGIMAGERELEASE(掩模);
uiimage*cutout=[uiimage imagewithcgimage:cututoref scale:[uiscreen mainscreen]scale]方向:uiimageoritionup];
CGIMAGERELEASE(剪切参考);
CGIMAGEREF SHADEDMASK=[自我创建掩码WITHSIZE:rect.SIZE形状:]^{
[[uicolor whitecolor]setfill];
cContextFillRect(uigraphicsgetCurrentContext(),rect);
cgContextSetsHadowWithColor(uigraphicsGetCurrentContext(),cgSizemake(0,1),1.0f,[[uicolor colorWithWhite:0.0 alpha:0.5]cgColor]);
[剪切绘图点:cgpointzero];
};
//创建负像
uigraphicsbeginimagecontextwithoptions(rect.size,no,0);
[[uicolor blackcolor]setfill];
//此处显示自定义形状
[文本绘制点:cgpointmake((self.bounds.size.width/2)-(font size.width/2),-1)带字体:字体];
uiimage*负=uigraphicsGetImageFromCurrentImageContext();
uigraphicsEndImageContext();
cImageRef innershadowRef=cImageCreateWithMask(negative.cgImage,shaddedmask);
CGIMAGERELEASE(阴影任务);
uiimage*innershadow=[uiimage imagewithcgimage:innershadowref scale:[uiscreen mainscreen]scale]方向:uiimageoritionup];
CGIMAGERELEASE(InnershadowRef);
//绘制实际图像
[[uicolor whitecolor]setfill];
[文本绘制点:cgpointmake((self.bounds.size.width/2)-(font size.width/2),-0.5),字体:字体];
[uicolor colorwithwhite:0.76 alpha:1.0]setfill];
[文本绘制点:cgpointmake((self.bounds.size.width/2)-(font size.width/2),-1)带字体:字体];
//最后应用阴影
[innershadow drawAtpoint:cgpointzero];
}
< /代码>
很容易倒转。我的解决方案是一个自定义视图,可以处理任何形状和文本。它需要iOS 4,并且与分辨率无关。
首先,对代码的作用进行图形解释。这里的形状是一个圆。
代码用白色的阴影绘制文本。如果不需要,可以进一步重构代码,因为Dropshadow需要以不同的方式进行屏蔽。如果您在旧版本的iOS上需要它,您必须替换块并使用(恼人的)cgBitmapContext。
- (UIImage*)blackSquareOfSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[[UIColor blackColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height));
UIImage *blackSquare = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blackSquare;
}
- (CGImageRef)createMaskWithSize:(CGSize)size shape:(void (^)(void))block {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
block();
CGImageRef shape = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(shape),
CGImageGetHeight(shape),
CGImageGetBitsPerComponent(shape),
CGImageGetBitsPerPixel(shape),
CGImageGetBytesPerRow(shape),
CGImageGetDataProvider(shape), NULL, false);
return mask;
}
- (void)drawRect:(CGRect)rect {
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:40.0f];
CGSize fontSize = [text_ sizeWithFont:font];
CGImageRef mask = [self createMaskWithSize:rect.size shape:^{
[[UIColor blackColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
[[UIColor whiteColor] setFill];
// custom shape goes here
[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];
[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];
}];
CGImageRef cutoutRef = CGImageCreateWithMask([self blackSquareOfSize:rect.size].CGImage, mask);
CGImageRelease(mask);
UIImage *cutout = [UIImage imageWithCGImage:cutoutRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGImageRelease(cutoutRef);
CGImageRef shadedMask = [self createMaskWithSize:rect.size shape:^{
[[UIColor whiteColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 1), 1.0f, [[UIColor colorWithWhite:0.0 alpha:0.5] CGColor]);
[cutout drawAtPoint:CGPointZero];
}];
// create negative image
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[[UIColor blackColor] setFill];
// custom shape goes here
[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];
UIImage *negative = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef innerShadowRef = CGImageCreateWithMask(negative.CGImage, shadedMask);
CGImageRelease(shadedMask);
UIImage *innerShadow = [UIImage imageWithCGImage:innerShadowRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGImageRelease(innerShadowRef);
// draw actual image
[[UIColor whiteColor] setFill];
[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -0.5) withFont:font];
[[UIColor colorWithWhite:0.76 alpha:1.0] setFill];
[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];
// finally apply shadow
[innerShadow drawAtPoint:CGPointZero];
}