【问题标题】:C4Label cuts off displayed textC4 标签截断显示的文本
【发布时间】:2013-10-08 17:07:44
【问题描述】:

我正在开发一个相当简单的应用程序,它应该从一个数组中随机显示 2 个彼此相邻的单词。现在它正在工作,除了...... 为什么有些文字被剪短了? 我猜想无论字符串“recipe”开头的字符数是多少,即使动态更改,它也可以容纳的最大字符数。因此,例如,当它以“Raw Taco”开头时,它会缩短“Boiled Hotdog”等较长的短语。最好的解决方法是什么?

这是我正在使用的代码(抱歉,它没有完全缩短为基本部分):

@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

【问题讨论】:

  • 你看过 NSMutableString 吗?

标签: ios objective-c c4


【解决方案1】:

您的代码中有答案,但已被注释掉。

[displaytask sizeToFit];

问题不在于字符串。您可以看到,当您记录它们时,它们是完整的。

问题在于显示它们的C4Label 以基于起始文本的框架开头。因此,当您从短文本开始时,显示的大小只有“Raw Taco”那么长,但是当“Rotten Sushi”出现时,它没有足够的空间来显示。当你调用sizeToFit 时,它会检查你需要多大的帧来保存text 中的当前字符串,并适当地更改C4Label 的帧。你可以阅读更多关于它的信息here

【讨论】:

    【解决方案2】:

    如果我理解正确的话,C4Label 是 UILabel 的子类吗?我的猜测是字符串被缩短了,因为框架中没有足够的空间让标签必须呈现整个字符串。所以,你有两个选择:

    1. 更改框架的大小或
    2. 缩小字体

    如果您想做 1,建议您使用 XIB 文件创建布局并适当设置大小限制。对于2,在标签上设置属性adjustsFontSizeToFitWidth为YES

    【讨论】:

    • 其实是C4Label包装了UILabel。
    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多