discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to know the geometry of a string on the screen ?


From: David Chisnall
Subject: Re: How to know the geometry of a string on the screen ?
Date: Sun, 29 Oct 2017 10:30:36 +0000

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.

Note: The -sizeWithAttributes: method will invoke the NSTypesetter / 
NSLayoutManager infrastructure under the hood (with some shared instances), but 
by calling it in this way doesn’t let you specify any constraints on the size, 
so you’re likely to end up with the length of the string with no wrapping or 
hyphenation.  This method is intended to let you quickly get an approximation 
of the size so that you can construct UI elements that you will then later 
resize once you know the real sizes.  If you want to know the size of a string 
with the correct line wrapping, then you should use the text system directly.

David




reply via email to

[Prev in Thread] Current Thread [Next in Thread]