Question

I'm working on a rather simple app that should display 2 words next to each other randomly from an array. Right now it's working except... Why is some text cut short? I'm guessing that whatever the number of chars the string "recipe" start with is the maximum number of chars it can hold even when changed dynamically. So, for example, when it starts with "Raw Taco", it will cut longer phrases like "Boiled Hotdog" short. What's the best workaround for this?

Here's the code I'm working with (sorry it's not fully shortened to essential bits):

@implementation C4WorkSpace

{
    C4Shape * red; //, *green, *blue;
    NSArray * foodz, *cookz;
    C4Label * displaytask;
    C4Sample *sample;
    NSString *recipe, *startRecipe;
    int COOKZtaskpicked;
    int FOODZtaskpicked;
}

-(void)setup{

    startRecipe = [NSString stringWithFormat:@"I want to make this really long to start with"];

    C4Font *font = [C4Font fontWithName:@"Avenir" size:40.0f];
    C4Label *label = [C4Label labelWithText:@"Order Up!" font:font];
    label.center = CGPointMake(self.canvas.center.x, self.canvas.height / 3.0f);

    [self.canvas addLabel:label];

    //[self setupShapes];
    //    [self setupLabels];
    red.fillColor = [UIColor colorWithRed:0.9f green:0.0f blue:0.0f alpha:0.7f];

    sample = [C4Sample sampleNamed:@"C4Loop.aif"];
    [sample prepareToPlay];


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \\
    // FOOD

    foodz =  @[ @"Sushi", @"Burger", @"Salad", @"Taco", @"Nachos", @"Hotdog" ];
    cookz =  @[ @"Baked", @"Boiled", @"Fried", @"Raw", @"Burnt", @"Rotten" ];
    [self changeFOODZtask]; // calls the function down below
    displaytask = [C4Label labelWithText:recipe];
    [displaytask addGesture:TAP name:@"tap" action:@"tapped:"];
    //[displaytask gestureForName:@"tap"]; // set double tap
    [self listenFor:@"tapped:" fromObject:displaytask andRunMethod:@"changeFOODZtask"];

    displaytask.center = self.canvas.center;
    //
    [self.canvas addSubview:displaytask];


    //displaytask = [C4Label labelWithText:cookz[0]];
    //[self changeCOOKZtask]; // calls the function down below
    //[displaytask addGesture:TAP name:@"tap" action:@"tapped:"];
    //[displaytask gestureForName:@"tap"]; // set double tap
    //[self listenFor:@"tapped:" fromObject:displaytask andRunMethod:@"changeCOOKZtask"];

    //displaytask.center = self.canvas.center;
    //
    //[self.canvas addSubview:displaytask];


}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \\
// Random FOODZ Picker
-(void) changeFOODZtask
{
    C4Log(@"changing task");
    FOODZtaskpicked = [C4Math randomIntBetweenA:0 andB: [foodz count] ];
    COOKZtaskpicked = [C4Math randomIntBetweenA:0 andB: [cookz count] ];

    recipe = [NSString stringWithFormat:@"%@ %@", cookz[COOKZtaskpicked],foodz[FOODZtaskpicked]];
    displaytask.text = recipe;
    C4Log(@"%@", recipe);

    [sample play];

    //    displaytask.text = foodz[FOODZtaskpicked];
    //    [displaytask sizeToFit];
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \\





@end
Was it helpful?

Solution

You have the answer in your code but it is commented out.

[displaytask sizeToFit];

The problem isn't with the strings. You can see that when you log them they are intact.

The problem is that the C4Label that displays them starts with a frame based on the starting text. So, when you start with short text the size of the display is only as long as "Raw Taco" but when "Rotten Sushi" comes up it doesn't have enough space to display. When you call sizeToFit what happens is that it will examine how big of a frame you need to hold the current string in text and changes the frame of the C4Label appropriately. You can read a bit more about it here.

OTHER TIPS

If I understand this correctly, C4Label is a UILabel subclass? My guess is the string is being cut short because there isn't enough space in the frame that the label has to render the whole string. So, you have two choices:

  1. Change the size of the frame or
  2. Make the font smaller

If you want to do 1, suggest you create your layout using a XIB file and set the sizing constraints appropriately. For 2, set the property adjustsFontSizeToFitWidth to YES on the label

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top