UILabel text truncates when rotated. How to fix?

I am creating a watermark to overlay on top of the image. Image can be expanded, enlarged, etc.

I created a watermark view class that, when sized, overlays a text label that is as large as within the borders. This works great and matches image sizes, etc.

Now I want to rotate the label. As soon as I apply the conversion, the text in the label is cut off. I guess I'm close, but I'm doing something stupid because I'm new to this. Any ideas?

Here is my class:

@interface WatermarkView () - (CGFloat)biggestBoldFontForString:(NSString*)text inRect:(CGRect)rect; @end @implementation WatermarkView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // create a label that will display the watermark mainLabel = [[UILabel alloc] initWithFrame:frame]; mainLabel.alpha = 0.35; mainLabel.backgroundColor = [UIColor clearColor]; mainLabel.text = @"watermark"; mainLabel.font = [UIFont boldSystemFontOfSize:[self biggestBoldFontForString:mainLabel.text inRect:mainLabel.frame]]; mainLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; // rotate label to a random slanted angle mainLabel.transform = CGAffineTransformMakeRotation(RANDOM_FLOAT(-M_PI/5, -M_PI/6)); [self addSubview:mainLabel]; } return self; } - (void)dealloc { [mainLabel release]; [super dealloc]; } - (CGFloat)biggestBoldFontForString:(NSString*)text inRect:(CGRect)rect { CGFloat actualFontSize = 200; [text sizeWithFont:[UIFont boldSystemFontOfSize:200] minFontSize:1 actualFontSize:&actualFontSize forWidth:rect.size.width lineBreakMode:0]; return actualFontSize; } - (void)layoutSubviews { [super layoutSubviews]; mainLabel.font = [UIFont boldSystemFontOfSize:[self biggestBoldFontForString:mainLabel.text inRect:self.frame]]; } @end 
+4
source share
2 answers

A simple solution may be to simply create a second label, such as your first, but adapted to the rotated view. Keep it hidden or alpha = 0, then when rotating it hides the source text and displays the new text.

Another problem may be that your frame is too small when it is rotated, try to keep the font of the right size and align to the center, but make the TextView frame a lot bigger than you think.

0
source

The reason the label truncates after the rotation transform is limited. The solution is to keep the label width before rotation and reassign after didRotateFromInterfaceOrientation.

eg.

 @synthesize labelWidth (void)viewDidLoad { self.labelWidth = _label.bounds.size.width; _label.transform = CGAffineTransformMakeRotation(-1.3f); } -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { CGRect bounds = _label.bounds; bounds.size.width = self.labelWidth; _label.bounds = bounds; } 
0
source

All Articles