是否可以使用 NSString UIKit 添加来绘制带有阴影的文本? [英] Is it possible to draw text with shadows using the NSString UIKit additions?

查看:10
本文介绍了是否可以使用 NSString UIKit 添加来绘制带有阴影的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 NSString UIKit 添加时是否可以使用简单的文本阴影进行绘制?我的意思是无需编写代码以两种颜色绘制两次,这可以通过各种 UIKit 类(例如 UILabel 及其 shadowColorshadowOffset 属性)完成,也无需进行实际模糊通过 CGContextSetShadow 的阴影(肯定会更昂贵).

Is it possible to draw with simple text shadows when using the NSString UIKit additions? I mean without writing code to draw twice in two colors, as can be done with various UIKit classes such as UILabel and its shadowColor and shadowOffset properties, and also without doing actual blur shadows via CGContextSetShadow (which are bound to be much more expensive).

Apple 的这些扩展的文档实际上包含常量(在最底部)包括 UITextAttributeTextShadowColorUITextAttributeTextShadowOffset 这意味着它是可能的,但我在实际方法中看不到它们的任何可能用法.

Apple's doc for these extensions actually include constants (at the very bottom) including UITextAttributeTextShadowColor and UITextAttributeTextShadowOffset which implies that it is possible, but I don't see any possible usage of these in the actual methods.

推荐答案

几个想法:

  1. UITextAttributeTextShadow... 键适用于使用文本属性字典时,例如,UIAppearance 方法:

  1. The UITextAttributeTextShadow... keys are intended for when using a text attribute dictionary with, for example, UIAppearance methods:

NSDictionary *attributes = @{UITextAttributeTextShadowColor  : [UIColor blackColor],
                             UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(2.0, 0.0)],
                             UITextAttributeTextColor        : [UIColor yellowColor]};

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

UITextAttributeTextShadow... 键仅用于那些接受文本属性字典的方法.

The UITextAttributeTextShadow... keys are designed for use only in those methods that accept a text attributes dictionary.

绘制文本字符串时最接近的等效键是使用带有 NSShadowAttributeName 键的属性字符串:

The closest equivalent key when drawing text strings is the use of attributed strings with the NSShadowAttributeName key:

- (void)drawRect:(CGRect)rect
{
    UIFont *font = [UIFont systemFontOfSize:50];

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor blackColor];
    shadow.shadowBlurRadius = 0.0;
    shadow.shadowOffset = CGSizeMake(0.0, 2.0);

    NSDictionary *attributes = @{NSShadowAttributeName          : shadow,
                                 NSForegroundColorAttributeName : [UIColor yellowColor],
                                 NSFontAttributeName            : font};

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:@"this has shadows" attributes:attributes];

    [attributedText drawInRect:rect];
}

如果您担心能够进行贝塞尔曲线阴影的阴影算法的性能影响,那么 NSShadow 可能会受到影响.但是做一些基准测试,更改 shadowBlurRadius 会显着影响性能.例如,在慢速 iPhone 3GS 上使用 shadowBlurRadius10.0 的复杂多行文本的旋转动画实现了 31 fps 的帧速率,但改变了 shadowBlurRadius0.0 的帧速率为 60 fps.

If you're concerned about the performance hit of a shadow algorithm capable of doing a bezier curve shadow, though, NSShadow may suffer from that. But doing some benchmarks, changing the shadowBlurRadius dramatically affects performance. For example, animating the rotation of a complex multi-line text with a shadowBlurRadius of 10.0 on a slow iPhone 3GS achieved a frame rate of 31 fps, but changing shadowBlurRadius to 0.0 yielded a frame rate of 60 fps.

底线,使用 0.0 的阴影模糊半径消除了贝塞尔生成阴影的大部分(如果不是全部)计算开销.

Bottom line, using a shadow blur radius of 0.0 eliminates most (if not all) of the computational overhead of a bezier generated shadow.

仅供参考,通过将 CGContextSetShadowblur 值设置为 0.0,我体验到了类似的性能提升,就像我经历过的一样上面的属性文本再现.

FYI, I experience a similar performance improvement by setting the blur value to 0.0 for CGContextSetShadow, just like I experienced with the attributed text rendition above.

最重要的是,只要您使用 0.0 的模糊半径,我认为您不必担心基于贝塞尔阴影的计算开销.如果你自己写两次文本,一次用于阴影,一次用于前景色,我不会感到惊讶,甚至可能更有效,但我不确定是否可以观察到差异.但我不知道有任何 API 调用可以为你做到这一点(除了 CGContextSetShadowblur of 0.0).

Bottom line, I don't think you should fear the computational overhead of bezier-based shadows as long as you use a blur radius of 0.0. I wouldn't be surprised if writing the text twice yourself, once for the shadow and again for the foreground color, might even be a bit more efficient, but I'm not sure if the difference will be observable. But I don't know of any API call that will do that for you (other than CGContextSetShadow with blur of 0.0).

这篇关于是否可以使用 NSString UIKit 添加来绘制带有阴影的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆