I have a problem too, my code is like this:
TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init]; [tagBtn setTitle:tagModel.values forState:UIControlStateNormal]; [tagBtn.titleLabel sizeToFit];
Subclass of TeacherTagBtn UIbutton you know! then I want to get the label width as follows:
tempBtn.titleLabel.frame.size.width
Finally, I failed. width is 0. so like this:
TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)]; [tagBtn setTitle:tagModel.values forState:UIControlStateNormal]; [tagBtn.titleLabel sizeToFit];
just add a frame, it works great!
source share