• 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
Strange behaviour of NSLayoutManager
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange behaviour of NSLayoutManager


  • Subject: Strange behaviour of NSLayoutManager
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Wed, 14 Aug 2013 20:43:02 +0700

The iPhone Simulator runs this code:

- (IBAction)fontSizeSliderHasMoved: sender;
{
	CGPoint contentOffset = self.textView.contentOffset;	//	we only use x-value, which is always zero
	NSTextContainer *textContainer = self.textView.textContainer;
	NSLayoutManager *layoutManager = self.textView.layoutManager;
	CALayer *layer = self.textView.layer;
	NSRange visibleGlyphRange = [ layoutManager glyphRangeForBoundingRect: layer.visibleRect inTextContainer: textContainer ];

	UIFont *currentFont = self.textView.font;
	self.textView.font = [ currentFont fontWithSize: self.sizeSlider.value ];

	NSRange a = NSMakeRange(0, NSMaxRange(visibleGlyphRange) );
	[ layoutManager ensureLayoutForGlyphRange: a ];

	CGRect r1 = [ layoutManager lineFragmentUsedRectForGlyphAtIndex: 	visibleGlyphRange.location
								effectiveRange: 						NULL
				];
	CGFloat lineHeight = r1.size.height;

	CGFloat strangeDiff;
	if 		( lineHeight < 10 )
	{
		strangeDiff = 0.9 * lineHeight;	//	must not be > lineHeight
	}
	else if ( lineHeight < 90 )
	{
		strangeDiff = 10 - 0.1 * lineHeight;
	}
	else
	{
		strangeDiff = 1;				//	must not be negative
	};

	contentOffset.y = r1.origin.y + strangeDiff;	//	← why do I need this?

	[ self.textView setContentOffset: contentOffset  animated: NO ];
}

The slider moves between 6 pt and 66 pt.

When I scroll the TextView so that any line is at the top, then using the slider will keep this same line fixed at the top of the TextView, which is exactly what I want.

But: why do I need this strangeDiff?

Obviously I am doing something very wrong and silly. But what?

Gerriet.


_______________________________________________

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


  • Follow-Ups:
    • Re: Strange behaviour of NSLayoutManager
      • From: Roland King <email@hidden>
  • Prev by Date: Re: Dismissing Open dlog before doc actually opens
  • Next by Date: Re: Can't type chars into UITextField?
  • Previous by thread: Visible range in UITextView
  • Next by thread: Re: Strange behaviour of NSLayoutManager
  • Index(es):
    • Date
    • Thread