4

I tried out NSMutableAttributedString to stylise my font with this code:

NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:element04.text];
    NSRange range = NSMakeRange(0, [attString length]);

    [attString addAttribute:NSFontAttributeName value:element04.font range:range];
    [attString addAttribute:NSForegroundColorAttributeName value:element04.textColor range:range];

    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:30/255 green:11/255 blue:0/255 alpha:1];
    shadow.shadowOffset = CGSizeMake(2.0f, 2.0f);
    [attString addAttribute:NSShadowAttributeName value:shadow range:range];

    [attString addAttribute:NSStrokeColorAttributeName value:[UIColor colorWithRed:30/255 green:11/255 blue:0/255 alpha:1] range:range];
    [attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-2.0] range:range];

    element04.attributedText = attString;

enter image description here

The issue here is that there are border at the shadow. How can I add border to the text with the shadow that has no border?

sooon
  • 4,718
  • 8
  • 63
  • 116

1 Answers1

0

It is certainly not what I would have expected. I suggest that you should file a bug report with Apple - though I suspect you'll be told this behavior is intentional. What's happening is that the stroke is casting a shadow of its own - and the weird part is that it is casting it on the fill, as if the layering order were:

  • Stroke
  • Shadow
  • Fill (foreground color)

You can make this much less annoying (and indeed rather pleasing) by adding a little shadowBlurRadius. In the image below, I pretty much used your code, but added this:

shadow.shadowBlurRadius = 3;

enter image description here

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the suggestion but I bet that is not up to me as I am developing for my client. – sooon Apr 30 '15 at 03:36