Is it possible to draw text with shadows using the NSString UIKit additions?(是否可以使用 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.
推荐答案
几个想法:
UITextAttributeTextShadow...
键适用于使用文本属性字典时,例如,UIAppearance
方法:
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 上使用 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
的阴影模糊半径消除了贝塞尔生成阴影的大部分(如果不是全部)计算开销.
Bottom line, using a shadow blur radius of 0.0
eliminates most (if not all) of the computational overhead of a bezier generated shadow.
仅供参考,通过将 CGContextSetShadow
的 blur
值设置为 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 调用可以为你做到这一点(除了 CGContextSetShadow
和 blur
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 添加来绘制带有阴影的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以使用 NSString UIKit 添加来绘制带有阴影的文本?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01