• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Fill box with repeating text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fill box with repeating text


  • Subject: Re: Fill box with repeating text
  • From: Trygve Inda <email@hidden>
  • Date: Sat, 25 Apr 2009 03:51:56 +0000
  • Thread-topic: Fill box with repeating text

> There are several ways to accomplish this.
>
> (I'll note that you can get the size of an attributed string using its
> -size method.)
>
> Option 1: Create an NSImage with the size returned from the attributed
> string. Draw the attributed string into the image. Create an NSColor
> pattern from the image, using colorWithPatternImage:. Fill the rect
> using [color set] and NSRectFill()/NSRectFillUsingOperation().
>
> Option 2: you could alternatively go the slightly lower-level Core
> Graphics route, and use CGContextDrawTiledImage(), which will draw a
> tiled CGImage. I'm using this method in my personal code. I can't
> remember why at the moment, but I may have run into limitations with
> option 1. (Of course, I'd definitely try option 1 first as it'll be
> easier.)
>
> David
>

Hi David,

This is pretty close:

NSImage*  theImage = [[NSImage alloc] initWithSize:[theString size]];

[theImage lockFocus];
[theString drawAtPoint:NSZeroPoint];
[theImage unlockFocus];

But I end up with black text on a white background. Adding:

NSColor*    textColor  = [NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0
alpha:0.50];

NSDictionary*   textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
textColor, NSForegroundColorAttributeName, nil];

NSAttributedString*    theString = [[NSAttributedString alloc]
initWithString:@"myText" attributes:textAttributes];

Makes the text transparent, but I can't seem to make the background of the
NSImage transparent. I want to end up with white text on a transparent
background, but [[NSImage alloc] initWithSize:[theString size]] seems to set
the background to solid white.

How can I fix this?

Thanks,

Trygve


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Fill box with repeating text
      • From: Chris Suter <email@hidden>
    • Re: Fill box with repeating text
      • From: Dave Keck <email@hidden>
References: 
 >Re: Fill box with repeating text (From: Dave Keck <email@hidden>)

  • Prev by Date: Conversation-style controls?
  • Next by Date: Re: Fill box with repeating text
  • Previous by thread: Re: Fill box with repeating text
  • Next by thread: Re: Fill box with repeating text
  • Index(es):
    • Date
    • Thread