40

在此处输入图像描述

我可以知道如何将浮雕效果作为图片上的“提醒”文字吗?

看起来文本是嵌入的?

谢谢

4

3 回答 3

90

iOS 7.0 更新

在 iOS 7.0 中,AppleNSTextEffectAttributeName为属性字符串添加了一个新属性 , 。如果您的部署目标是 iOS 7.0 或更高版本,您可以将此属性设置为NSTextEffectLetterpressStyle以浮雕样式绘制属性字符串。

原来的

我不能确定苹果是如何绘制浮雕文字的。在我看来,他们用微红色填充字符串字形,然后在字形的内部边缘应用阴影,并在字形的顶部外边缘应用非常微弱的阴影。我试了一下,它是这样的:

iPhone模拟器屏幕截图

最上面是我的渲染。下面是一个简单的带有阴影的 UILabel,正如 Chris 在他的回答中所建议的那样。我在后台放了一张提醒应用程序的屏幕截图。

这是我的代码。

首先,您需要一个创建字符串图像掩码的函数。您将使用遮罩来绘制字符串本身,然后绘制仅出现在字符串内部边缘周围的阴影。该图像只有一个 Alpha 通道,没有 RGB 通道。

- (UIImage *)maskWithString:(NSString *)string font:(UIFont *)font size:(CGSize)size
{
    CGRect rect = { CGPointZero, size };
    CGFloat scale = [UIScreen mainScreen].scale;
    CGColorSpaceRef grayscale = CGColorSpaceCreateDeviceGray();
    CGContextRef gc = CGBitmapContextCreate(NULL, size.width * scale, size.height * scale, 8, size.width * scale, grayscale, kCGImageAlphaOnly);
    CGContextScaleCTM(gc, scale, scale);
    CGColorSpaceRelease(grayscale);
    UIGraphicsPushContext(gc); {
        [[UIColor whiteColor] setFill];
        [string drawInRect:rect withFont:font];
    } UIGraphicsPopContext();

    CGImageRef cgImage = CGBitmapContextCreateImage(gc);
    CGContextRelease(gc);
    UIImage *image = [UIImage imageWithCGImage:cgImage scale:scale orientation:UIImageOrientationDownMirrored];
    CGImageRelease(cgImage);

    return image;
}

其次,您需要一个反转该掩码的函数。您将使用它来使 CoreGraphics 在字符串的内边缘周围绘制阴影。这需要是完整的 RGBA 图像。(iOS 似乎不支持灰度+alpha 图像。)

- (UIImage *)invertedMaskWithMask:(UIImage *)mask
{
    CGRect rect = { CGPointZero, mask.size };
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, mask.scale); {
        [[UIColor blackColor] setFill];
        UIRectFill(rect);
        CGContextClipToMask(UIGraphicsGetCurrentContext(), rect, mask.CGImage);
        CGContextClearRect(UIGraphicsGetCurrentContext(), rect);
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

您可以在将字符串绘制为红色并将阴影应用到其内部边缘的函数中使用它们。

-(UIImage *)imageWithInteriorShadowAndString:(NSString *)string font:(UIFont *)font textColor:(UIColor *)textColor size:(CGSize)size
{
    CGRect rect = { CGPointZero, size };
    UIImage *mask = [self maskWithString:string font:font size:rect.size];
    UIImage *invertedMask = [self invertedMaskWithMask:mask];
    UIImage *image;
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale); {
        CGContextRef gc = UIGraphicsGetCurrentContext();
        // Clip to the mask that only allows drawing inside the string's image.
        CGContextClipToMask(gc, rect, mask.CGImage);
        // We apply the mask twice because we're going to draw through it twice.
        // Only applying it once would make the edges too sharp.
        CGContextClipToMask(gc, rect, mask.CGImage);
        mask = nil; // done with mask; let ARC free it

        // Draw the red text.
        [textColor setFill];
        CGContextFillRect(gc, rect);

        // Draw the interior shadow.
        CGContextSetShadowWithColor(gc, CGSizeZero, 1.6, [UIColor colorWithWhite:.3 alpha:1].CGColor);
        [invertedMask drawAtPoint:CGPointZero];
        invertedMask = nil; // done with invertedMask; let ARC free it

        image = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    return image;
}

接下来,您需要一个函数来获取图像并返回带有微弱向上阴影的副本。

- (UIImage *)imageWithUpwardShadowAndImage:(UIImage *)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); {
        CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, -1), 1, [UIColor colorWithWhite:0 alpha:.15].CGColor);
        [image drawAtPoint:CGPointZero];
    }
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

最后,您可以结合这些功能来创建字符串的浮雕图像。我将我的最终图像放入一个UIImageView以便于测试。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect rect = self.imageView.bounds;
    NSString *string = @"Reminders";
    UIFont *font = [UIFont systemFontOfSize:33];
    UIImage *interiorShadowImage = [self imageWithInteriorShadowAndString:string
        font:font
        textColor:[UIColor colorWithHue:0 saturation:.9 brightness:.7 alpha:1]
        size:rect.size];
    UIImage *finalImage = [self imageWithUpwardShadowAndImage:interiorShadowImage];

    self.imageView.image = finalImage;
}
于 2011-12-11T22:31:00.760 回答
4

这看起来就像文本周围的阴影。您在 IB 中设置文本颜色的同一区域中设置它 - 为阴影选择合适的颜色并设置您想要的阴影偏移量(在您发布的示例中,看起来他们将阴影颜色设置为相同的颜色作为文本并将其偏移 0 水平和 -1 垂直(这意味着向上一个像素)。

在代码中,属性设置如下(假设您已经设置了一个名为“label”的 UILabel:

label.shadowColor = [UIColor blackColor]; // Choose your color here - in the example
                                          // posted, they probably chose a similar color
                                          // to the text color and then set the alpha
                                          // down around 0.7 or so so the shadow would be
                                          // faint.
label.shadowOffset = CGSizeMake(0,-1);    // First parameter is horizontal, second is vertical
于 2011-12-11T21:43:20.127 回答
4

你可以在这个例子的基础上配置你的效果 在此处输入图像描述

于 2015-07-11T17:45:36.133 回答