0

I'm feeling very stupid right now.

I'm trying to draw text onto a UIViewController.

I'm using Xcode 5 and started with a simple Single Page project, and have done nothing else except adding this code to the ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CGRect myRect=CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
    UIFont *myFont=[UIFont fontWithName:@"Helvetica" size:50];

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = NSTextAlignmentRight;

    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject:myFont forKey:NSFontAttributeName];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attributes setObject:[NSNumber numberWithFloat:4] forKey:NSStrokeWidthAttributeName];
    [attributes setObject:[UIColor whiteColor] forKey:NSStrokeColorAttributeName];
    [@"test" drawInRect:myRect withAttributes:attributes];

    // draw fill
    [attributes removeObjectForKey:NSStrokeWidthAttributeName];
    [attributes removeObjectForKey:NSStrokeColorAttributeName];
    [attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
    [@"test" drawInRect:myRect withAttributes:attributes];
}

It runs without errors but produces no text - why?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Are you just doing this as an exercise for learning (good for you if so)? Otherwise, you can just put a `UILabel` in the view and let that take care of drawing your text. – Tim Reddy Apr 14 '14 at 20:46
  • Yes, trying to learn - I've had troubles with UILabels cropping the Descenders off fonts,so I thought I'd see if I could draw the text myself and eliminate the problems. – wayneh Apr 14 '14 at 20:52

2 Answers2

0

The drawInRect method doesn't work with UIViewController. That method is a UIView method and will only work if your class is a UIView class or subclass.

So, as mentioned above, you can just set your text attributes on a UILabel and display them wherever you like on the screen. It should give you the same effect. Optionally, you could try to change the subclass to UIView, but that could require more modifications to your code than you would like.

tfrank377
  • 1,858
  • 2
  • 22
  • 34
  • So if I wanted to use the automatically created View in my UIViewController that would work? If so, how would I use the Rect in the View? Or can I add an ImageView? – wayneh Apr 14 '14 at 22:08
  • I've had no luck using the automatically created view such as `self.view`, or a `UIImageView` with the `drawInRect` method. You can do some drawing on a `UIImageView` with Core Graphics, but that is probably not what you are looking for with drawing text. – tfrank377 Apr 15 '14 at 05:03
0

To add to tfrank377, you can add labels programmatically with something like

   UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
   [self.view addSubview:addLabel];
   addLabel.text = @"Hello World";

which will display text on your UIView.

flooie
  • 89
  • 2
  • 8
  • Thanks but I'm trying something besides UILabels due to other errors described above. – wayneh Apr 14 '14 at 22:10
  • Can you give an example of a descender being cut off? – flooie Apr 14 '14 at 22:13
  • Sure - here's another question I've asked. Note that the point is not to shrink the font until the problem goes away - the point is to display the largest font possible...http://stackoverflow.com/questions/22897158/fonts-not-fitting-properly-in-uilabel-ascender-or-descender-cropped – wayneh Apr 15 '14 at 13:18