iOS中button设置多行

在最近写的一个Demo中需要button显示两行,我尝试了给button写分类,重新自定义button,后来写到一半想想设置默认和选中的颜色时会好麻烦,能不能像label那样方便了,突然想起来有titlelabel 的属性,尝试代码如下

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

 button.titleLabel.numberOfLines = 0;

button.titleLabel.font = [UIFont systemFontOfSize:14];

[button setTitle:@"昨天\n9/26" forState:UIControlStateNormal];

这样设置就可以了,给他赋值的时候中间加一个\n换行

效果如下:

iOS中button设置多行_第1张图片

你可能感兴趣的:(iOS中button设置多行)