- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想在我的一个具有 3dish 外观的屏幕上呈现一些文本。我正在使用 UIKit 和标准 View Controller 等。
效果看起来像这样:
这可以通过 UIKit 和 iOS 以某种方式完成吗?通常我只会使用静态 png 但是,文本是动态的并且会根据用户数据进行更新
最佳答案
嗯,这是一个基本示例。
我们的想法是通过 x/y 1 偏移反复绘制相同文本的图层以创建“深度”外观。
我已经创建了一个名为 UIImage+3d 的 UIImage 类别,您可以对其进行测试:
这是头文件(.h)
//
// UIImage+3D.h
//
// Created by Lefteris Haritou on 12/10/12.
// Feel Free to use this code, but please keep the credits
//
#import <UIKit/UIKit.h>
@interface UIImage (Extensions)
+ (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth useShine:(BOOL)_shine;
@end
这是实现(.m)文件
//
// UIImage+3D.m
//
// Created by Lefteris Haritou on 12/10/12.
// Feel Free to use this code, but please keep the credits
//
#import "UIImage+3D.h"
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
@implementation UIImage (Extensions)
+ (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth useShine:(BOOL)_shine {
//calculate the size we will need for our text
CGSize expectedSize = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
//increase our size, as we will draw in 3d, so we need extra space for 3d depth + shadow with blur
expectedSize.height+=_depth+5;
expectedSize.width+=_depth+5;
UIColor *_newColor;
UIGraphicsBeginImageContextWithOptions(expectedSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
//because we want to do a 3d depth effect, we are going to slightly decrease the color as we move back
//so here we are going to create a color array that we will use with required depth levels
NSMutableArray *_colorsArray = [[NSMutableArray alloc] initWithCapacity:_depth];
CGFloat *components = (CGFloat *)CGColorGetComponents(_foregroundColor.CGColor);
//add as a first color in our array the original color
[_colorsArray insertObject:_foregroundColor atIndex:0];
//create a gradient of our color (darkening in the depth)
int _colorStepSize = floor(100/_depth);
for (int i=0; i<_depth; i++) {
for (int k=0; k<3; k++) {
if (components[k]>(_colorStepSize/255.f)) {
components[k]-=(_colorStepSize/255.f);
}
}
_newColor = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:CGColorGetAlpha(_foregroundColor.CGColor)];
//we are inserting always at first index as we want this array of colors to be reversed (darkest color being the last)
[_colorsArray insertObject:_newColor atIndex:0];
}
//we will draw repeated copies of our text, with the outline color and foreground color, starting from the deepest
for (int i=0; i<_depth; i++) {
//change color
_newColor = (UIColor*)[_colorsArray objectAtIndex:i];
//draw the text
CGContextSaveGState(context);
CGContextSetShouldAntialias(context, YES);
//draw outline if this is the last layer (front one)
if (i+1==_depth) {
CGContextSetLineWidth(context, 1);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetTextDrawingMode(context, kCGTextStroke);
[_outlineColor set];
[_text drawAtPoint:CGPointMake(i, i) withFont:_font];
}
//draw filling
[_newColor set];
CGContextSetTextDrawingMode(context, kCGTextFill);
//if this is the last layer (first one we draw), add the drop shadow too and the outline
if (i==0) {
CGContextSetShadowWithColor(context, CGSizeMake(-2, -2), 4.0f, _shadowColor.CGColor);
}
else if (i+1!=_depth){
//add glow like blur
CGContextSetShadowWithColor(context, CGSizeMake(-1, -1), 3.0f, _newColor.CGColor);
}
[_text drawAtPoint:CGPointMake(i, i) withFont:_font];
CGContextRestoreGState(context);
}
//if we need to apply the shine
if (_shine) {
//create an alpha mask from the top most layer of the image, so we can add a shine effect over it
CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, (int)expectedSize.width, (int)expectedSize.height, 8, (int)expectedSize.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
UIGraphicsPushContext(imageContext);
CGContextSetTextDrawingMode(imageContext, kCGTextFill);
[_text drawAtPoint:CGPointMake(_depth-1, _depth-1) withFont:_font];
CGImageRef alphaMask = CGBitmapContextCreateImage(imageContext);
CGContextRelease(imageContext);
UIGraphicsPopContext();
//draw shine effect
//clip context to the mask we created
CGRect drawRect = CGRectZero;
drawRect.size = expectedSize;
CGContextSaveGState(context);
CGContextClipToMask(context, drawRect, alphaMask);
CGContextSetBlendMode(context, kCGBlendModeLuminosity);
size_t num_locations = 4;
CGFloat locations[4] = { 0.0, 0.4, 0.6, 1};
CGFloat gradientComponents[16] = {
0.0, 0.0, 0.0, 1.0,
0.6, 0.6, 0.6, 1.0,
0.8, 0.8, 0.8, 1.0,
0.0, 0.0, 0.0, 1.0
};
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(genericRGBColorspace, gradientComponents, locations, num_locations);
CGPoint start = CGPointMake(0, 0);
CGPoint end = CGPointMake(0, expectedSize.height);
CGContextDrawLinearGradient(context, glossGradient, start, end, 0);
CGColorSpaceRelease(genericRGBColorspace);
CGGradientRelease(glossGradient);
CGImageRelease(alphaMask);
CGContextRestoreGState(context);
}
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
@end
要使用它,只需导入类别扩展,然后按如下方式使用它:
UIImage *my3dImage = [UIImage create3DImageWithText:@"3" Font:[UIFont systemFontOfSize:250] ForegroundColor:[UIColor colorWithRed:(200/255.f) green:(200/255.f) blue:(200/255.f) alpha:1.0] ShadowColor:[UIColor blackColor] outlineColor:[UIColor colorWithRed:(225/255.f) green:(225/255.f) blue:(225/255.f) alpha:1.0] depth:8 useShine:NO];
UIImageView *imgView = [[UIImageView alloc] initWithImage:my3dImage];
[self.view addSubview: imgView];
另一个例子是这样的:
UIImage *my3dImage = [UIImage create3DImageWithText:@"3D" Font:[UIFont fontWithName:@"MarkerFelt-Wide" size:180] ForegroundColor:[UIColor colorWithRed:(222/255.f) green:(100/255.f) blue:(100/255.f) alpha:1.0] ShadowColor:[UIColor blackColor] outlineColor:[UIColor colorWithRed:(216/255.f) green:(120/255.f) blue:(120/255.f) alpha:1.0] depth:6 useShine:NO];
UIImageView *imgView = [[UIImageView alloc] initWithImage:my3dImage];
imgView.center = self.view.center;
[self.view addSubview: imgView];
结果是这样的:
我编辑了代码以在图像上添加闪光效果,我相信这会让它看起来更酷
UIImage *my3dImage = [UIImage create3DImageWithText:@"3D" Font:[UIFont fontWithName:@"MarkerFelt-Wide" size:180] ForegroundColor:[UIColor colorWithRed:(222/255.f) green:(100/255.f) blue:(100/255.f) alpha:1.0] ShadowColor:[UIColor blackColor] outlineColor:[UIColor colorWithRed:(216/255.f) green:(120/255.f) blue:(120/255.f) alpha:1.0] depth:6 useShine:YES];
UIImageView *imgView = [[UIImageView alloc] initWithImage:my3dImage];
imgView.center = self.view.center;
[self.view addSubview: imgView];
关于iphone - iOS 中的 3d 文字效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13766268/
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我试图用这种形式简单地获取数字 28 integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s 但我得到了两个数字11 和
最近一直在学习D语言。我一直对运行时感到困惑。 从我能收集到的关于它的信息中,(这不是很多)我知道它是一种有助于 D 的一些特性的运行时。像垃圾收集一样,它与您自己的程序一起运行。但是既然 D 是编译
想问一下这两个正则表达式有区别吗? \d\d\d 与 \d{3} 我已经在我的本地机器上使用 Java 和 Windows 操作系统对此进行了测试,两者都工作正常并且结果相同。但是,当在 linux
我正在学习 Go,而且我坚持使用 Go 之旅(exercise-stringer.go:https://tour.golang.org/methods/7)。 这是一些代码: type IPAddr
我在Java正则表达式中发现了一段令我困惑的代码: Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); 要编译的字符串是: String string
我在 ruby 代码上偶然发现了这个。我知道\d{4})\/(\d\d)\/(\d\d)\/(.*)/是什么意思,但是\1-\2-\3-\4 是什么意思? 最佳答案 \1-\2-\3-\4 是 b
我一直在努力解决这个问题,这让我很恼火。我了解 D 运行时库。它是什么,它做什么。我也明白你可以在没有它的情况下编译 D 应用程序。就像 XoMB 所做的那样。好吧,XoMB 定义了自己的运行时,但是
我有两个列表列表,子列表代表路径。我想找到所有路径。 List> pathList1 List> pathList2 当然是天真的解决方案: List> result = new ArrayList>
我需要使用 Regex 格式化一个字符串,该字符串包含数字、字母 a-z 和 A-Z,同时还包含破折号和空格。 从用户输入我有02-219 8 53 24 输出应该是022 198 53 24 我正在
目标是达到与this C++ example相同的效果: 避免创建临时文件。我曾尝试将 C++ 示例翻译为 D,但没有成功。我也尝试过不同的方法。 import std.datetime : benc
tl;dr:你好吗perfect forwarding在 D? 该链接有一个很好的解释,但例如,假设我有这个方法: void foo(T)(in int a, out int b, ref int c
有什么方法可以在 D 中使用abstract auto 函数吗? 如果我声明一个类如下: class MyClass { abstract auto foo(); } 我收到以下错误: mai
有没有人为内存中重叠的数组切片实现交集?算法在没有重叠时返回 []。 当 pretty-print (使用重叠缩进)内存中重叠的数组切片时,我想要这个。 最佳答案 如果您确定它们是数组,那么只需取 p
我已经开始学习 D,但我在使用 Andrei Alexandrescu 所著的 The D Programming Language 一书中提供的示例时遇到了一些麻烦。由于 int 和 ulong 类
如何创建一个不可变的类? 我的目标是创建一个实例始终不可变的类。现在我只是用不可变的方法和构造函数创建了一个“可变”类。我将其称为 mData,m 表示可变。然后我创建一个别名 alias immut
不久前我买了《The D Programming Language》。好书,很有教育意义。但是,我在尝试编译书中列出的语言功能时遇到了麻烦:扩展函数。 在这本书中,Andrei 写了任何可以像这样调用
我在 D http://www.digitalmars.com/d/2.0/lazy-evaluation.html 中找到了函数参数的惰性求值示例 我想知道如何在 D 中实现可能的无限数据结构,就像
这个问题在这里已经有了答案: 12 年前关闭。 Possible Duplicate: Could anyone explain these undefined behaviors (i = i++
当前是否可以跨模块扫描/查询/迭代具有某些属性的所有函数(或类)? 例如: source/packageA/something.d: @sillyWalk(10) void doSomething()
我是一名优秀的程序员,十分优秀!