iOS Button title换行居中。

来源:互联网 发布:linux执行器 编辑:程序博客网 时间:2024/05/28 23:21

效果如图:   

                  

代码:

            NSString *  title     = @"11-28*8:00";

            NSArray *  stringArr  = [titlecomponentsSeparatedByString:@"*"];

            NSString *  endTitle  = [NSStringstringWithFormat:@"%@\n%@",stringArr[0],stringArr[1]];

            selectBtn.titleEdgeInsets =UIEdgeInsetsMake(0,20, 0,0);

            selectBtn.titleLabel.numberOfLines =0;

            [selectBtn setTitle:endTitleforState:UIControlStateNormal];


思路:

1.    首先把字符串按你想要的分割法分成数组

2.    然后重新拼接字符串,数组之间用/n连接

3.    记得设置numberOfLines   还有这时候 button的titleLabel  textAlignment 设置居中没有用的。要设置titleEdgeInsets


1 0