 |
 |
floating point errors in NSTextView and NSImage?
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
I want to put text into an NSView I have done so, with success, by making a NSTextView a subview to my view and setting the size of the font and stuff by using commands like setFont:.
When I resize the window my view resizes and tells everything in it to resize also. The problem that I have run into is that NSImages and any NSTextViews resize as if there is some sort of floating point rounding error happening. I can slowly drag my window bigger and it looks like they kind of jump. They sort of drift a little bit as I resize, and then after I resize a certain amount, they jump into place. Below them I have drawn a checkerboard pattern and it resizes without jumping. Is there some sort of a known problem with NSImages and NSTextViews or is it quite likely that I am just doing something wrong?
The second thing that I have been wondering is how I can draw text directly into my view without having to use an NSTextView. I saw the methods in NSString but nowhere does it say what the attributed string should be like.
Any help on either of these issues would be appreciated.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Status:
Offline
|
|
There used to be some nice little PSshow(), PSsetfont(),... functions but they went away. Now you use NSAttributedStrings. If you want to see a simple example, I have a free calendar:
http://softrak.stepwise.com/Apps/Web...1377&os=20
for a link to the download. It is a basic custom view that is subclassed from NSImageView. It includes some basic labels and numbers...
Note theCatagory for NSDictionary. The method is a bit heavy for many purposes, but points you in the right direction:
@implementation NSDictionary (FoundationExtend)
+(NSDictionary *) getAttributeWithFont  NSFont *) font color  NSColor *) color
{
NSArray *objects;
NSArray *keys;
NSMutableParagraphStyle *paragraphStyle;
float offset = [font pointSize];
//
// Setup the paragraph style
//
paragraphStyle = [[[NSMutableParagraphStyle defaultParagraphStyle]
mutableCopy]
autorelease];
[paragraphStyle setMaximumLineHeight  ffset * .9];
objects = [NSArray arrayWithObjects:
font,
color,
paragraphStyle,
// [NSNumber numberWithFloat  ffset * .65],
nil];
keys = [NSArray arrayWithObjects:
NSFontAttributeName,
NSForegroundColorAttributeName,
NSParagraphStyleAttributeName,
// NSBaselineOffsetAttributeName,
nil];
return ([NSDictionary dictionaryWithObjects  bjects forKeys:keys]);
}
@end
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Thanks for the help with displaying strings. I found out what the problem I had was with my stuff jumping around. It is, kind of, a floating point thing. It seems that I was being fooled by Apple's antialiasing. When the objects that I drew with NSBezier path and similar things were resized, they would accept a floating point pixel value and show it by antialiasing the result. images, on the other hand, don't use any antialias equivalent so when they move they go to the nearest pixel.
BTW, I learned that you can turn off antialiasing using:
CGContextSetShouldAntialias([[NSGraphicsContext currentContext] graphicsPort], NO);
in your drawRect method.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
The second thing that I have been wondering is how I can draw text directly into my view without having to use an NSTextView. I saw the methods in NSString but nowhere does it say what the attributed string should be like.
The drawing methods are declared in AppKit/NSStringDrawing.h. The base NSAttributedString is defined in Foundation/NSAttributedString.h, but the specific attributes that the AppKit recognizes are in AppKit/NSAttributedString.h.
The string drawing methods are probably the best thing to use, but it is also possible to use NSTextFieldCells to do your drawing too. Simply create an NSTextFieldCell, set the font and whichever other settings are desired, then call [cell drawWithFrame:myDesiredTextArea inView:self] from your view's -drawRect: method. Using NSTextFieldCells is probably slightly more inefficient than using the NSStringDrawing stuff directly, but it's hardly noticeable. It's certainly far more efficient than using an NSTextField as a subview, which in turn is more efficient than using an NSTextView as a subview (NSTextView is a big object).
An NSTextFieldCell represents most of the functionality behind an NSTextField, with the exception that it's not an NSView itself (and therefore much lighter-weight) but rather is told when and where to display itself by an owning NSView. All NSCell subclasses operate on this principle, which can help make NSView subclasses easier to write and more efficient. NSTextField is really just a simple wrapper around a single NSTextFieldCell, while NSMatrix is a wrapper around a whole grid of them (or any NSCell subclass) which is way more efficient than having a whole bunch of nested NSViews.
There used to be some nice little PSshow(), PSsetfont(),... functions but they went away.
I believe there are CoreGraphics equivalents to these (CGContextShowText(), CGContextSelectFont(), etc). However, the text handling in CoreGraphics -- and [Display]PostScript before that, meaning PSshow() and friends -- doesn't handle Unicode and several other text-handling attributes. They should only be used when you know the limitations are OK and you really really really need the performance (or don't have access to the Cocoa and/or Carbon APIs)
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|