How to size a TextView to fit a given line?
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