Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabels in UIScrollView misaligned despite using CGRectIntegral?

I have a UILabel with an attributed string that I'm creating in a UIScrollView:

CGRect frame = self.view.frame;
frame.origin.x = 10;
frame.size.width -= 10;
frame = CGRectIntegral(frame);

UILabel *textView = [[UILabel alloc] initWithFrame:frame];
textView.numberOfLines = 0;
textView.lineBreakMode = NSLineBreakByWordWrapping;
textView.backgroundColor =[UIColor clearColor];
[self addSubview:textView];

Despite ensuring the frame is set using CGRectIntegral, the iOS Simulator still shows the label as misaligned.

Misaligned label

To try to get rid of the misalignment, I also tried this without any luck:

textView.frame = CGRectIntegral(textView.frame);
textView.bounds = CGRectIntegral(textView.bounds);

Can a UILabel with attributed strings be properly aligned in scrollView?

like image 998
memmons Avatar asked Oct 10 '13 21:10

memmons


1 Answers

I'm not sure it will solve your problem, but you have nothing to lose to try another method :

CGRect frame = CGRectOffset(CGRectInset(self.view.frame,5,0),5,0);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = @"Test";
[self addSubview:label];

If it's not working the error may come from your label itself (maybe adjustsFontSizeToFitWidth, lineBreakMode, sizeToFit or contentMode). All your label code is here ?

Try an incremental debugging because it may not come from the frame at all.

like image 181
Francescu Avatar answered Nov 17 '22 16:11

Francescu