Quantcast
Channel: Tom's Blog » .NET
Viewing all articles
Browse latest Browse all 41

[Xamarin iOS] How to pre-calculate the size of a text, depending of its content ?

$
0
0

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!

 

Happy coding!


Viewing all articles
Browse latest Browse all 41

Latest Images

Trending Articles





Latest Images