iphone - 为什么我的 UILabel 没有被更改?

标签 iphone cocoa uilabel

为什么我的 UILabel 没有被更改?我正在使用以下代码,但没有任何反应:

- (void)awakeFromNib {
    percentCorrect.adjustsFontSizeToFitWidth;
    percentCorrect.numberOfLines = 3;
    percentCorrect.minimumFontSize = 100;
}

这是我的实现代码:

- (void) updateScore {
    double percentScore = 100.0 * varRight / (varWrong + varRight);
    percentCorrect.text = [NSString stringWithFormat:@"%.2f%%", percentScore];
}

- (void)viewDidLoad {
    percentCorrect.adjustsFontSizeToFitWidth = YES;
    percentCorrect.numberOfLines = 3;
    percentCorrect.minimumFontSize = 100;
    percentCorrect.text = @"sesd";
}


- (void)correctAns {
    numberRight.text = [NSString stringWithFormat:@"%i Correct", varRight];
}

-(void)wrongAns {
    numberWrong.text = [NSString stringWithFormat:@"%i Incorrect", varWrong];
}

#pragma mark Reset Methods
- (IBAction)reset:(id)sender; {
    NSString *message = @"Are you sure you would like to reset?";
    self.wouldYouLikeToReset = [[UIAlertView alloc] initWithTitle:@"Reset?" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    [wouldYouLikeToReset addButtonWithTitle:@"Continue"];
    [self.wouldYouLikeToReset show];
    // Now goes to (void)alertView and see's what is being pressed!
}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
    {
        NSLog(@"Cancel button pressed");
    }
    else
    {
        varRight = 0;
        varWrong = 0;
        [self wrongAns];
        [self correctAns];
        percentCorrect.text = [NSString stringWithFormat:@"0.0%%"];

    }
}

#pragma mark Button Action Methods

- (IBAction)right:(id)sender; {
    varRight++;
    [self correctAns];
    [self updateScore];
}

- (IBAction)wrong:(id)sender; {
    varWrong++;
    [self wrongAns];
    [self updateScore];
}


- (IBAction)subOneRight:(id)sender {
    if (varRight > 0 ) {
        varRight--;
        [self correctAns];
        [self updateScore];
    }
}


- (IBAction)subOneWrong:(id)sender {
    if (varWrong > 0) {
        varWrong--;
        [self wrongAns];
        [self updateScore];
    }
}

-(IBAction)addHalfCredit:(id)sender;
{
    varWrong++;
    varRight++;
    [self wrongAns];
    [self correctAns];
    [self updateScore];
}


@end

有什么想法吗? 谢谢

最佳答案

  1. 为了使 adjustsFontSizeToFitWidth 设置生效,numberOfLines 属性必须设置为 1。如果 != 1,则不起作用.

  2. awakeFromNibviewDidLoadviewWillAppear是否被调用?

  3. 如果文本适合当前字体的当前边界,则 minimumFontSize 属性将不会执行任何操作。您是否设置了标签的字体属性?

    percentCorrect.font = [UIFont systemFontOfSize:20];

  4. 最后,对于最小字体大小来说,minimumFontSize = 100 是不是有点太大了?

关于iphone - 为什么我的 UILabel 没有被更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/957918/

相关文章:

iphone - 如何在iPhone中实现条形图?

iphone - 为什么我的 ViewController 仅在第二次调用后释放,iOS ARC?

objective-c - Objective C - 如何动态添加/删除数组结构

python - 从任何(不安全)字符串创建(正常/安全)应用程序包标识符

iphone - 如何[[NSBundle mainBundle] objectForInfoDictionaryKey : @"CFBundleVersion"]; works?

ios - 如何增加 UILabel 中的字符间距

ios - X 和 Y 值未应用于标签

ios - 从函数 : Unexpectedly found nil while unwrapping an Optional value 设置 UILabel

iphone - UIButton 标记未在 cellForRowAtIndexPath 中正确设置

iphone - UIScrollView w/zoom 在缩放之前不会滚动