我想在我的一个屏幕上渲染一些文本,它看起来像3dish。我使用的是UIKit和标准视图控制器等。
效果将如下所示:
这可以通过UIKit & iOS以某种方式实现吗?通常我只会使用静态的png,但是,文本是动态的,并根据用户数据进行更新
发布于 2012-12-11 04:08:01
下面的代码可能并不完美,但它应该是一个很好的起点。
基本上,您绘制字体两次,稍微更改大小和偏移量。根据你正在处理的字体和大小,你可能不得不使用一些fontSize
,fontSizeDelta
和fontOffset
。
结果看起来有点像这样:
- (UIImage *)imageWith3dString:(NSString *)text
{
CGFloat fontSize = 150.0;
CGFloat fontSizeDelta = 3.0;
CGFloat fontOffset = 5.0;
NSString *fontName = @"Bebas";
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
CGSize size = CGSizeMake(textSize.width, fontSize);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL,
(int)size.width,
(int)size.height,
8,
(int)(4 * size.width),
colorSpace,
kCGImageAlphaPremultipliedLast);
// Draw with shadow
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), 10.0, [UIColor colorWithWhite:0.0 alpha:0.6].CGColor);
CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 0.6);
CGContextSetAllowsAntialiasing(ctx, YES);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
CGContextSetRGBFillColor(ctx, 222 / 255.0, 222 / 255.0, 222 / 255.0, 1.0);
CGContextSetCharacterSpacing(ctx, 2.6);
CGContextSelectFont(ctx, [fontName UTF8String], fontSize - fontSizeDelta, kCGEncodingMacRoman);
CGContextShowTextAtPoint(ctx, 0.0, 3.0 + fontOffset, [text UTF8String], text.length);
CGContextSetShadowWithColor(ctx, CGSizeZero, 0.0, NULL); // disable shadow
CGContextSetCharacterSpacing(ctx, 1.0);
CGContextSelectFont(ctx, [fontName UTF8String], fontSize, kCGEncodingMacRoman);
CGContextShowTextAtPoint(ctx, 0.0, 3.0, [text UTF8String], text.length);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
CGContextRelease(ctx);
return image;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[self imageWith3dString:@"3"]];
[self.view addSubview:imageView];
}
YMMV
https://stackoverflow.com/questions/13766268
复制相似问题