ios – 是否可以使用NSString UIKit添加的阴影绘制文本?

使用NSString UIKit添加时,是否可以使用简单的文字阴影绘制?我的意思是没有编写代码来绘制两种颜色,可以使用各种UIKit类(如UILabel及其shadowColor和shadowOffset属性)进行绘制,也可以通过CGContextSetShadow(这势必要贵一些)实现实际的模糊阴影.

Apple’s doc for these extensions实际上包括常量(在最底层),包括UITextAttributeTextShadowColor和UITextAttributeTextShadowOffset,这意味着它是可能的,但是我在实际的方法中没有看到任何可能的用法.

解决方法

几个想法:

> UITextAttributeTextShadow …键用于在使用文本属性字典时使用例如UIAppearance方法:

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

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

UITextAttributeTextShadow …键设计仅用于接受文本属性字典的那些方法.
>绘制文本字符串时最接近的等效键是使用带有NSShadowAttributeName键的归因字符串:

- (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的帧速率.

使用阴影模糊半径0.0的底线消除了贝塞尔生成阴影的大部分(如果不是全部)计算开销.
> FYI,通过将CGContextSetShadow的模糊值设置为0.0,我也经历了类似的性能提升,就像我在上面的归因文本转换一样.

底线,我不认为你应该害怕基于贝塞尔的阴影的计算开销,只要您使用0.0的模糊半径.我不会惊讶,如果自己写两次文本,一次为阴影,再次为前景色,甚至可能会更有效率,但我不知道差异是否可以观察到.但是我不知道会为你做什么API调用(除了CGContextSetShadow,模糊为0.0).

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...