7

现在我正在使用

NSShadow *textShadow = [NSShadow new];
textShadow.shadowBlurRadius = 5;
textShadow.shadowColor = [[NSColor whiteColor] colorWithAlphaComponent:.5];

[self addAttribute:NSShadowAttributeName value:textShadow range:NSMakeRange(0, self.length)];

从 NSTextStorage 给文本一个阴影。但我想应用多个阴影,添加另一个NSShadowAttributeName只会覆盖以前的值。

如何添加多个阴影?可以用CGContextSetShadowWithColor吗?

4

2 回答 2

3

不确定请为您的文本视图尝试以下代码。当您在 textview 中写入字符串时,它将选择那么多范围并在此基础上绘制颜色:-

-(IBAction)createNewTabView:(id)sender
{
   NSString *allTheText =[tv string];
    NSArray *lines = [allTheText componentsSeparatedByString:@"\n"];
    NSString *str=[[NSString alloc]init];
    NSMutableAttributedString *attr;
    BOOL isNext=YES;
    [tv setString:@""];
    for (str in lines)
    {
        attr=[[NSMutableAttributedString alloc]initWithString:str];
        if ([str length] > 0)
        {

        NSRange range=NSMakeRange(0, [str length]);
        [attr addAttribute:NSBackgroundColorAttributeName value:[NSColor greenColor] range:range];
        [tv .textStorage appendAttributedString:attr];
            isNext=YES;
        }
        else
        {
            NSString *str=@"\n";
            NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
            [tv .textStorage appendAttributedString:attr];
            isNext=NO;
        }
        if (isNext==YES)
        {
            NSString *str=@"\n";
            NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
            [tv .textStorage appendAttributedString:attr];

        }

    }
    }
于 2013-10-10T15:06:44.763 回答
2

我建议采用“穴居人”方法。不要试图获得重复的阴影,而是使用重复的文本,除了其中一个之外,所有的都将其颜色设置为清晰的颜色,并给它们不同的 dropShadows。

  • 制作两个或多个完全对齐的文本。
  • 使最前面成为您的“真实”文本颜色。
  • 使其他文本的颜色清晰。
  • 为每种类型的文本设置一种类型的阴影。

如果您要多次使用它,您应该能够创建一个自动执行此操作的类。

这是我得到的样本:

在此处输入图像描述

哦——如果你不添加额外的文本,你的文本会比你预期的更暗/更粗。

于 2013-10-12T06:13:51.763 回答