[Xamarin iOS] How to pre-calculate the size of a text, depending of its content ?December 7th, 2016 | Posted by in .NET | Xamarin | Read: 1,535
When doing Xamarin.iOS (or just iOS) development, you often need a way to compute the size of a text, dependding of its content.
Well, in fact, this is pretty simple thanks to the class NSString and its method StringSize:
var titleFont = UIFont.FromName(ApplicationFonts.LightFontName, 15f); var requestedSize = new NSString("Put your text here").StringSize(titleFont);
The method StringSize takes in parameter the font that will be applied to the text, and all the dedicated parameters (bold, italic, etc.). Indeed, according to the kind of font and the style applied, the height/width will not be the same every time.
Simple but works fine!