代码之家  ›  专栏  ›  技术社区  ›  Dan Ray

为什么这个按钮不显示它的文本标签?

  •  8
  • Dan Ray  · 技术社区  · 15 年前

    所有关于这个UIButton的东西都很好,除了应该在上面的文本。NSLog证明文本位于正确的位置。有什么好处?

    UIButton *newTagButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [newTagButton addTarget:self action:@selector(showNewTagField) forControlEvents:UIControlEventTouchUpInside];
    newTagButton.titleLabel.text = @"+ New Tag";
    NSLog(@"Just set button label to %@", newTagButton.titleLabel.text);
    newTagButton.titleLabel.font = [UIFont systemFontOfSize:17];
    newTagButton.titleLabel.textColor = [UIColor redColor];
    CGSize addtextsize = [newTagButton.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];
    CGSize buttonsize = { (addtextsize.width + 20), (addtextsize.height * 1.2) };
    newTagButton.frame = CGRectMake(x, y, buttonsize.width, buttonsize.height);
    [self.mainView addSubview:newTagButton];
    
    1 回复  |  直到 15 年前
        1
  •  20
  •   Russell Zornes    15 年前

    UIButton上有一组api,应该用来更改这些属性。

    [button setTitle:title forState:state];
    [button setTitleColor:color forState:state];
    [button setTitleShadowColor:color forState:state];
    

    应该始终通过这些方法(如果可用)设置这些属性,而不是直接触摸标题标签。对于字体,您可以直接在标题标签上更改它,因为它们不提供UIButton上的方法。