[NSTextView sizeToFit] messing up frame.origin, and not resizing correctly
[NSTextView sizeToFit] messing up frame.origin, and not resizing correctly
- Subject: [NSTextView sizeToFit] messing up frame.origin, and not resizing correctly
- From: Phillip Hutchings <email@hidden>
- Date: Tue, 10 Feb 2004 16:28:44 +1300
I'm (still) developing a screensaver that draws text to the screen from
an NSTextView. I've been having ongoing problems with positioning and
sizing. The text is changed once every few minutes (it grabs track info
from iTunes), which may be a different length to the current text, so I
need to resize the textview to take account of this.
As the text is supposed to move across the screen, I need to know how
wide and high the text is so I can make sure it doesn't run over the
edge of the screen.
Firstly, the textview is horizontally and vertically resizable. This is
my current resize code, which is called whenever the text changes, with
the work-around for the problems I'm having:
Notes:
theAttributes is an NSArray with all the text attributes
theResult is an NSAppleEventDescriptor from executing an NSAppleScript
theTextStorage is the NSTextView's textStorage object (it's easier to
replace the text this way, so far anyway)
NSAttributedString *trackInfoString = [[NSAttributedString alloc]
initWithString:[theResult stringValue] attributes:theAttributes];
[theTextStorage setAttributedString: trackInfoString];
NSSize stringSize = [trackInfoString size];
NSRect frame = [self frame];
NSRect newFrame = NSMakeRect(0,0,0,0);
newFrame.origin.x = viewFrame.origin.x;
newFrame.origin.y = viewFrame.origin.y;
if (stringSize.width+20 < frame.size.width-artworkSize.width-20) {
NSLog(@"Size less: %f <
%f",stringSize.width+20,frame.size.width-artworkSize.width-20);
newFrame.size.width = ceilf(stringSize.width+20);
newFrame.size.height = ceilf(frame.size.height);
} else {
NSLog(@"Size greater: %f >
%f",stringSize.width+20,frame.size.width-artworkSize.width-20);
newFrame.size.width = ceilf(frame.size.width-artworkSize.width-20);
newFrame.size.height = ceilf(frame.size.height);
}
[trackTextView setFrame:newFrame];
[trackTextView sizeToFit];
[trackTextView setFrameOrigin:newFrame.origin];
The two main problems I'm having are:
sizeToFit: won't decrease the width. This is quite important as it
determines the total area I have to move. So I set it from the
NSAttributedString's size if it is smaller than the screen area, minus
the image space (plus 20 to account for borders etc). If the string is
longer, I set it to the maximum area available. The height is always
the full screen height so it line-wraps properly.
Secondly, after calling sizeToFit: the size still isn't always right as
it doesn't take the first line in to account, and the origin has some
large number (around 800) added to it. I think the number is the screen
height (768 in my case). The ceilf's are there because I was wondering
if it was a problem coping with the decimal places, but it doesn't seem
so.
Currently it's working, but I'm sure I'm doing something wrong here.
--
Phillip Hutchings
email@hidden
http://www.sitharus.com/
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.