• 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
How to size a TextView to fit a given line?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to size a TextView to fit a given line?


  • Subject: How to size a TextView to fit a given line?
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Sun, 12 Aug 2012 16:34:48 +0700

In windowWillUseStandardFrame:defaultFrame: in a subclass of NSDocument (which is also the delegate of it's window) I want to set the window to just contain a certain line.

- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame
{
	//	myTextView is an NSTextView inside a scroll view, which is the sole window content

	NSString *mySpecialLine = @"... something...";
	NSDictionary *typingAttributes = [ myTextView typingAttributes ];
	NSFont *typingFont = [ typingAttributes objectForKey: NSFontAttributeName ];

	NSSize oneSize = [ mySpecialLine sizeWithAttributes: typingAttributes ];
	//	this does not work - it always uses printer fonts even if myTextView does not.

	//	so I tried:
	NSLayoutManager *layoutManager = [ myTextView layoutManager ] ;
	BOOL usesScreenFonts = [ layoutManager usesScreenFonts ];
	NSStringDrawingOptions options = NSStringDrawingOneShot;
	if ( !usesScreenFonts ) options |= NSStringDrawingDisableScreenFontSubstitution;
	NSRect oneRect = [ mySpecialLine boundingRectWithSize: NSMakeSize(9999,99) options: options attributes: typingAttributes ];
	//	but I still get the metrics of the printer font.

	CGFloat lineWidth = ...
	//	but even when I get the length of mySpecialLine I have to add a magic value of 10 to get the appropriate frame size of myTextView
	CGFloat textExtra = 10;	//	???

	//	then there is some difference between window.frame.size.width and myTextView (this is not a problem)
	NSRect 	textFrame = [ theText frame ];
	CGFloat windowExtra = winFrame.size.width - textFrame.size.width;

	newWindowRect.size.width = lineWidth + textExtra + windowExtra;
	return newWindowRect;
}

How to get the size of mySpecialLine with screen fonts?
How do I avoid hardcoding a special value of 10?

10.8, but should work on 10.7.4 as well.

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: How to size a TextView to fit a given line?
      • From: Ross Carter <email@hidden>
  • Prev by Date: Re: 32-bit on 10.8
  • Next by Date: Re: PubSub Framework Alternative
  • Previous by thread: Re: Forcing IB in Xcode 4.4 to notice a change
  • Next by thread: Re: How to size a TextView to fit a given line?
  • Index(es):
    • Date
    • Thread