老师你好,我第一次尝试,只设置对应状态的文字。
第一次运行发现好像没有字,点击之后发现应该是没有设置UIButton背景颜色。
但是我想的是,不改背景颜色,而是该字的颜色,然后大概选了个比较像的方法。
……
@property(nonatomic,strong,readwrite) UIButton * deleteButton;
……
self.deleteButton.titleLabel.textColor = [UIColor blackColor];
但是改了之后仍然没有生效,那么是不是因为 titleLabel 并不是 UILabelView 对于文字实现的 View , 还是有其他原因,看头文件没有源码,无法判断。那这种情况下应该怎么操作呢 ?
展开
作者回复: UILabel是无状态的,但是UIButton是有状态的哈,所以在设置文字或者颜色的时候,需要根据button不同的状态设置不同的颜色,所以要使用UIButton提供的方法,当状态切换时(比如选中、高亮),系统内部会reset相应的属性~对于颜色要使用 - (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use opaque white