次のコードがあります:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 25, 25);
[[button layer] setCornerRadius:5.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];
[button.titleLabel setFrame:CGRectMake(0,0, 25, 25)];
[button setTitle:[NSString stringWithFormat:@"%@", [[topics objectAtIndex:indexPath.row] unread]] forState:UIControlStateNormal];
問題は、テキスト内の文字列が長くない場合は問題なく表示されることです (1 ~ 2 桁)。ただし、文字列がかなり長い場合 (3 桁以上)、赤いボタンだけが表示され、テキストは表示されません。これを調整するにはどうすればよいですか?
私はそうは思わない:
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
役に立ちますよね?
ベストアンサー1
これを試して:
button.titleLabel?.numberOfLines = 1
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = .byClipping //<-- MAGIC LINE
なぜこれがうまくいくのかはわかりませんが、うまくいくのです :)