我想制作一个 UIButtonTypeCustom 类型的 UIButton(用于形状)。我想使用 button.titleLabel 分配标题,因为我需要指定字体。下面的代码看起来应该可以工作,但没有——没有标签出现,句号。
UIImage *editButtonImage = [UIImage imageNamed: @"editButton.png"];
float width = editButtonImage.size.width;
float height = editButtonImage.size.height;
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
每个人总是说要使用 setTitle:forState: 但这会给你一种我不喜欢的字体。不推荐使用 titleLabel 方法——它应该可以工作。
我之前遇到过几次并且总是解决它,但我真的很想弄清楚。有任何想法吗?