views:

52

answers:

4

We have a window filled with little view squares (think of a Calculator).

For a specific view on the window we want display a single string in the view without using the Interface Builder to add the string. We need to be able to change the string and have the view refresh.

How do we programmatically add a string to a view and show it?


Update:

Ok here is the code we have currently. Nothing special in the header file. I suppose the real quandry is considering we can easily get the background color to change, why is it that our text is just not showing?? Both versions are in there, would be happy to get 'apples' or 'oranges' displaying.

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {

  bgString = @"orange";

  UILabel* aLabel = [[UILabel alloc] init];
  aLabel.text = @"apple";
  self.textLabel = aLabel;
  [aLabel release];
  [self addSubview:textLabel];
}
return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code 
  [[UIColor yellowColor] setFill]; 
  UIRectFill(rect);
  [self drawStringCenteredIn:rect];
}

- (void)drawStringCenteredIn:(CGRect)r {
  //CGSize strSize = [bgString size];
  CGPoint strOrigin;
  strOrigin.x = r.origin.x; //+ (r.size.width - 10)/2;
  strOrigin.y = r.origin.y; //+ (r.size.height - 10)/2;
  //[bgString drawAtPoint:strOrigin withFont:[UIFont fontWithName:@"Helvetica" size:10]];
  [textLabel drawTextInRect:r];
}
A: 

How about creating a UILabel and adding it to the view?

St3fan
beat me by 8 seconds, +1
cobbal
How do we do that programmatically?
Evolve
St3fan posted this while I was typing up my solution -- see my answer.
Shaggy Frog
+4  A: 

In your view controller's .h:

@interface MyViewController
{
    UILabel* label;
}

@property (nonatomic, retain) UILabel* label;

In your view controller's .m:

- (void)dealloc
{
    [label release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel* aLabel = [[UILabel alloc] init];
    aLabel.text = @"Initial Text";
    self.label = aLabel;
    [aLabel release];
    [self.view addSubview:aLabel];
}

- (void)viewDidUnload
{
    [self.label removeFromSuperview];
    self.label = nil;
}

// Call this when you need to update the label
- (void)updateLabel
{
    self.label.text = @"Some updated text";
}

Did that from memory but it should work.

Shaggy Frog
don't forget `[aLabel sizeToFit]`
cobbal
Hmm. Also forgot to init it with a frame -- hoping the OP can kinda take this and run with it.
Shaggy Frog
Thanks for everyone's quick responses so far.This would work well if I was working with the ViewController, but Im specifically wanting to interact with a 'view' placed on the main ViewController. So I need my code to go in a drawRect type structure.
Evolve
If you can't get there from here you are going to have a rough time I think. Replace [self.view addsubview:] with whatever view you actually want to add it to...
Kendall Helmstetter Gelner
I would highly advice against exposing that label as a property. What would be the point?
St3fan
@St3fan I agree; I would normally expose it inside the .h as a property as a "private" method. However, please consider the audience. The OP doesn't know how to add a label to a view, so introducing that concept is too much too soon.
Shaggy Frog
A: 

If you subclass the UIView, you can draw your string in the view's drawRect. This allows great flexibility in modifying the text, its appearance, and its placement (you can even animate it around, spin, rotate, etc.)

Call setNeedsDisplay on the view after you change your NSString. Then do an drawAtPoint:withFont: on the NSString when the drawRect is called.

hotpaw2
While you are correct that this approach allows "great flexibility", subclassing `UIView` and overriding `drawRect:` is probably the wrong tool for this job.
Shaggy Frog
+1  A: 

Try this:

UILabel* aLabel = [[UILabel alloc] initWithFrame:[self bounds]];

If you are creating the label manually, you need to set it's frame manually too. Frame itself is size and position inside parent view(superview). In my example i've set the frame of label to occupy the entire view. If you need your custom size you can use:

UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,height)];

Where (x,y) - position of the top left corner of your label.

Anton Chikin