|
Q: How can I draw a string that specifies both a fill color and a stroke width?A: Use a negative value for The sign of the value for Figure 1: A zero value for stroke width indicates fill with no stroke. Figure 2: With a positive value for stroke width the string is stroked but not filled. The sample code in Listing 1 shows how a custom view might both fill and stroke an attributed string in its Listing 1: Specifying that a string is to be drawn with both fill and stroke. - (void)drawRect:(NSRect)rect { NSString *string = @"Got Fill?"; NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary]; // Set a font and specify a "fill" color [stringAttributes setObject: [NSFont fontWithName:@"Arial-Black" size:64] forKey: NSFontAttributeName]; [stringAttributes setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName]; // Set a negative width so we get both stroke and fill to show [stringAttributes setObject: [NSNumber numberWithFloat: -2.0] forKey: NSStrokeWidthAttributeName]; [stringAttributes setObject: [NSColor blackColor] forKey: NSStrokeColorAttributeName]; // Now paint the background [[NSColor grayColor] set]; [NSBezierPath fillRect: [self bounds]]; // And finally draw the string with these attributes [string drawAtPoint: NSMakePoint(100, 100) withAttributes: stringAttributes]; } Figure 3: A negative value for stroke width will produce both fill and stroke. See Also For more information on drawing strings, you may wish to refer the String Programming Guide for Cocoa, the Attributed Strings Programming Guide and Cocoa Drawing Guide: Text. Document Revision History
Posted: 2008-03-25 |
|