24

I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method.

The text is drawn but it is always black no matter what I set the color to.

- (void)drawRect:(CGRect)rect
{
    UIFont* font = [UIFont fontWithName:@"Arial" size:72];
    UIColor* textColor = [UIColor redColor];
    NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Hello" attributes:stringAttrs];

    [attrStr drawAtPoint:CGPointMake(10.f, 10.f)];
}

I've also tried [[UIColor redColor] set] to no avail.

Answer:

NSDictionary* stringAttrs = @{ NSFontAttributeName : font, NSForegroundColorAttributeName : textColor };

RobertJoseph
  • 7,968
  • 12
  • 68
  • 113

2 Answers2

22

Instead of UITextAttributeTextColor you should use NSForegroundColorAttributeName. Hope this helps!

Levi
  • 7,313
  • 2
  • 32
  • 44
  • 1
    Thanks. I also should have been using: NSFontAttributeName – RobertJoseph Dec 15 '12 at 17:24
  • 2
    That's correct. Here you can find the [list](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/Reference/Reference.html#//apple_ref/doc/uid/TP40004007) of all the keys to use in the attributes dictionary – Levi Dec 15 '12 at 17:30
4

You can try with following methods. It will help you to draw text in UIView at the bottom right corner with the help of following attributes.

  • NSFontAttributeName - Font name with size
  • NSStrokeWidthAttributeName - Stroke Width
  • NSStrokeColorAttributeName - Text Color

Objective-C - Draw text in the UIView and return as UIImage.

    -(UIImage *) imageWithView:(UIView *)view text:(NSString *)text {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);

        [view.layer renderInContext:UIGraphicsGetCurrentContext()];

        // Setup the font specific variables
        NSDictionary *attributes = @{
                NSFontAttributeName   : [UIFont fontWithName:@"Helvetica" size:12],
                NSStrokeWidthAttributeName    : @(0), 
                NSStrokeColorAttributeName    : [UIColor blackColor]
        };
        // Draw text with CGPoint and attributes
        [text drawAtPoint:CGPointMake(view.frame.origin.x+10 , view.frame.size.height-25) withAttributes:attributes];

        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return img;
    }`

Swift - Draw text in the UIView and return as UIImage.

    func imageWithView(view : UIView, text : NSString) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        view.layer.renderInContext(UIGraphicsGetCurrentContext()!);
        // Setup the font specific variables
        let attributes :[String:AnyObject] = [
            NSFontAttributeName : UIFont(name: "Helvetica", size: 12)!,
            NSStrokeWidthAttributeName : 0,
            NSForegroundColorAttributeName : UIColor.blackColor()
        ]
        // Draw text with CGPoint and attributes
        text.drawAtPoint(CGPointMake(view.frame.origin.x+10, view.frame.size.height-25), withAttributes: attributes);
        let img:UIImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        return img;
    }`
Undo
  • 25,519
  • 37
  • 106
  • 129
Vignesh Kumar
  • 598
  • 4
  • 11