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

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

问题描述

使用NSString UIKit添加时,是否可以使用简单的文本阴影进行绘制?我的意思是没有编写代码以两种颜色绘制两次,可以使用各种UIKit类,如UILabel及其 shadowColor shadowOffset 属性,也没有通过 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的这些扩展文档实际上包括常量(在最底层),包括 UITextAttributeTextShadowColor UITextAttributeTextShadowOffset 这意味着这是可能的,但我在实际方法中没有看到任何可能的用法。

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.

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

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];
}

如果您担心影子算法的性能损失能够做到但是,一个bezier曲线阴影, NSShadow 可能会受此影响。但是做一些基准测试,更改 shadowBlurRadius 会显着影响性能。例如,在慢速iPhone 3GS上使用 shadowBlurRadius 10.0 动画复杂多行文本的旋转动画帧速率为31 fps,但将 shadowBlurRadius 更改为 0.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 消除了大部分(如果不是全部)计算开销一个bezier生成的阴影。

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

仅供参考我通过将 blur 值设置为以下来实现类似的性能提升对于 CGContextSetShadow 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.

底线,我不认为你应该担心基于bezier的阴影的计算开销,只要你使用<$ c $的模糊半径C> 0.0 。如果你自己两次写文本,一次用于阴影再用于前景色,我也不会感到惊讶,甚至可能会更有效率,但我不确定差异是否可以观察到。但我不知道会为你做任何API调用( CGContextSetShadow blur 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天全站免登陆