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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With