我想创建一个类型为UIButtonTypeCustom的UIButton(用于形状)。我想使用button.titleLabel来指定标题,因为我需要指定字体。下面的代码看起来应该可以工作,但不会 - 没有标签显示,句点。iOS:UIButton 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方法不被弃用 - 它应该起作用。
我已经遇到过这几次,总是只是解决它,但我真的很想弄明白。有任何想法吗?
所以,你不能使用titleLabel设置的东西是实际的文本值。这对我来说似乎有点奇怪,但没关系。 – Amagrammer 2011-02-05 23:59:15