objective-c - 自定义按钮的标题

标签 objective-c ios

我正在创建一个像这样的自定义 UIButton :

IButton *buttonDetailCarte = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonDetailCarte.frame = CGRectMake(8, 223, 304, 44);
        [buttonDetailCarte setBackgroundImage:[UIImage imageNamed:@"cellule.png"] forState:UIControlStateNormal];
        buttonDetailCarte.titleLabel.text = @" my Text";
        buttonDetailCarte.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17];
        buttonDetailCarte.titleLabel.textColor = [UIColor colorWithRed:84.0/255 green:84.0/255 blue:84.0/255 alpha:1.0f];
        buttonDetailCarte.backgroundColor = [UIColor clearColor];
        buttonDetailCarte.titleLabel.textAlignment = UITextAlignmentLeft;

问题是我的文本没有出现在我的按钮中,当我用 IB 创建相同的按钮时,它出现了?有什么问题

最佳答案

你应该使用 setTitle:forControlState:方法更改按钮文本,而不是直接操作按钮的 titleLabel。

像您一样操作 titleLabel 的其他属性,也不推荐。尝试使用 setTitleColor: 代替。但是,titleLabel 的 font 等其他属性可以更改。一开始会感到困惑,但您会习惯的!

关于objective-c - 自定义按钮的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519041/

相关文章:

iphone - 将数据传递给容器 View

ios - 多选问题并在 UITableview 中快速选择全部

ios - 定期在 UITextField 触发方法

ios - 在 ios Objective-c 的 mapview 上添加带有多个图像的多个注释?

ios - 我可以使用新的 @import 指令导入自定义框架吗?

objective-c - 向 UIViewController 及其所有子类添加属性

ios - cocos2D中的CCProgressTimer

objective-c - 等到首选项 Pane 完全可见才能显示工作表?

objective-c - iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?

iphone - 带有动画启动画面的iPhone/iPad应用