首页 > 代码库 > label同时设置sizeToFit,NSTextAlignmentCenter不起作用

label同时设置sizeToFit,NSTextAlignmentCenter不起作用

问题:label要多行显示,按照这样子设置,iOS9以上work,iOS8无用

self.bookNameLabel.lineBreakMode = NSLineBreakByCharWrapping;    self.bookNameLabel.numberOfLines = 0;    self.bookNameLabel.textAlignment = NSTextAlignmentCenter;

然后据说要加 sizeToFit:好吧,依然然并卵

然后据说要同时加font,好吧,多行出来了,但是不居中

然后stackOverFlow:https://stackoverflow.com/questions/28579776/nstextalignmentcenter-for-uilabel-not-working/35066864#35066864?newreg=725179871da349d698d31b72bdbb3565

技术分享

work!

最终:

self.bookNameLabel.font =[UIFont systemFontOfSize:18];    self.bookNameLabel.lineBreakMode = NSLineBreakByCharWrapping;    self.bookNameLabel.numberOfLines = 0;    self.bookNameLabel.textAlignment = NSTextAlignmentCenter;    [self.bookNameLabel sizeToFit];    self.bookNameLabel.frame = CGRectMake((self.frame.size.width-self.bookNameLabel.frame.size.width)/2, self.bookNameLabel.frame.origin.y, self.bookNameLabel.frame.size.width, self.bookNameLabel.frame.size.height);

 

label同时设置sizeToFit,NSTextAlignmentCenter不起作用