• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: auto-layout and rotated UILabels
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: auto-layout and rotated UILabels


  • Subject: Re: auto-layout and rotated UILabels
  • From: Steve Christensen <email@hidden>
  • Date: Sat, 06 Feb 2016 18:16:40 -0800

On Feb 4, 2016, at 2:59 PM, Quincey Morris <email@hidden> wrote:
>
> On Feb 4, 2016, at 13:01 , Steve Christensen <email@hidden <mailto:email@hidden>> wrote:
>>
>> it looks like the width of the embedding view is set to the text width of the UILabel instead of the text height, which is borne out by the view geometry
>
> Can you use a custom class for the embedding view and override its ‘intrinsicContentSize’ property to return the inner view’s height and width reversed?

Quincey: Thanks for a starting point.

I spent this afternoon playing around with it some more. Overriding -intrinsicContentSize does result in a correctly sized bounds, but the frame is still invalid and the view contents are still drawn incorrectly.

I finally found a simple solution that avoids all the headache of trying to get auto layout to play nicely with a transformed UILabel: I don't apply a transform to the UILabel. Instead I subclass UILabel and override two of its methods: -textRectForBounds:limitedToNumberOfLines: (which -intrinsicContentSize calls) and -drawTextInRect:.


- (CGRect) textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
	CGRect	textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

	return CGRectMake(textRect.origin.y, textRect.origin.x, textRect.size.height, textRect.size.width);
}

- (void) drawTextInRect:(CGRect)rect
{
	CGContextRef		context		= UIGraphicsGetCurrentContext();
	CGRect			bounds		= self.bounds;
	CGFloat			translateX	= bounds.size.width / 2;
	CGFloat			translateY	= bounds.size.height / 2;
	CGAffineTransform	transform	= CGAffineTransformIdentity;

	transform = CGAffineTransformTranslate(transform, translateX, translateY);
	transform = CGAffineTransformRotate(transform, (CGFloat)-M_PI_2);
	transform = CGAffineTransformTranslate(transform, -translateX, -translateY);

	CGContextSaveGState(context);
	CGContextConcatCTM(context, transform);

	rect = CGRectApplyAffineTransform(rect, transform);
	[super drawTextInRect:rect];

	CGContextRestoreGState(context);
}

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >auto-layout and rotated UILabels (From: Steve Christensen <email@hidden>)
 >Re: auto-layout and rotated UILabels (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: WebViews and the Java Plugin
  • Next by Date: Strange Analyser Warning/Error
  • Previous by thread: Re: auto-layout and rotated UILabels
  • Next by thread: How to know if an NSView has pending draws
  • Index(es):
    • Date
    • Thread