|
From: | Bertrand Gmail |
Subject: | Re: How to know the geometry of a string on the screen ? |
Date: | Sun, 29 Oct 2017 12:58:40 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
Le 29/10/2017 à 11:30, David Chisnall a écrit :
Thanks for your answer. So I can't just use a method of NSString on an NSattributedString.On 27 Oct 2017, at 20:34, Bertrand Gmail <bertrand.dekoninck@gmail.com> wrote:Le 27/10/2017 à 20:36, Josh Freeman a écrit :I think you meant to use 'string', not '[NSString string]' - the latter returns an empty string. stringSize=[[NSString string] sizeWithAttributes:attributes]; -> stringSize=[string sizeWithAttributes:attributes];I introduced [NSString string] because of this warning at compile time : GSPanel.m:75:23: warning: 'NSMutableAttributedString' may not respond to 'sizeWithAttributes:' stringSize=[string sizeWithAttributes:attributes]; And the executable doesn't work.But now you are asking what the length of an empty string is, when a set of attributes are applied. You want to be asking what the length of @“GNUstep” is. You do not need to construct an NSAttributedString to call a method on NSString.
I just wanted to put a centered label as a subview of a window (a simple UI element indeed), using a boldSystemFont attribute. So I needed to calculate the size of the attributed string. And I don't need line wrapping.
Josh's "[string size]" advice seems to be the way to go.Moreover, I could put "string.size" instead of "[string size]". But I wonder if it ist an Objc-2 feature and if it will compile with gcc and it's libobjc.
I use somewhere else in my code : "screenFrame = [[NSScreen mainScreen] frame]; screenSize = screenFrame.size;" Is it also Objc-2 only ?I ask because I wanted to be Objc-1 only and I tried "[screenFrame size]" instead but it doesn't work : I've got this message from the compiler :
"error: bad receiver type 'NSRect' (aka 'struct _NSRect') screenSize = [screenFrame size];"That's not a big deal because I already I had another more complicated code for this borrowed from the internet. It's my will to understand what's going wrong there.
Cheers, Bertrand
[Prev in Thread] | Current Thread | [Next in Thread] |