Alex Sorokoletov
Alex Sorokoletov

Reputation: 3101

Xamarin.iOS NSString.StringSize wrong dimensions (sizeWithFont)

I'm trying to calculate how much space string will take on screen. I'm using NSString.StringSize (which now points to sizeWithFont:constrainedToSize:lineBreakMode: in Xamarin.iOS)

UIFont MeasureFont = UIFont.SystemFontOfSize(16f);
private static SizeF MaxMeasureSize = new SizeF(208, float.MaxValue);
//...
string messageBody = "Test";//comes from my dataSource
NSString nsString = new NSString(messageBody);
var textSize = nsString.StringSize(MeasureFont, MaxMeasureSize, UILineBreakMode.CharacterWrap);
Console.WriteLine("Measured -- {0} for [{1}]", textSize, messageBody);

Output in my app:

2014-09-05 00:05:20.338 App1[3150:90b] Measured -- {Width=29.68, Height=19.088} for [Add]

2014-09-05 00:05:20.339 App1[3150:90b] Measured -- {Width=29.488, Height=19.088} for [Test]

Screenshot with this font and these strings in PhotoShop. Clearly, string 'Test' is longer than 'Add'

enter image description here

Upvotes: 1

Views: 1703

Answers (0)

Related Questions