I am looking for a way to convert from UIFont’s size to Android TextView’s textSize attribute, for example:
[UIFont fontWithName:@"HelveticaNeue" size:14];
The iOS Dev Doc specifies the unit of the UIFont size attribute as point, however on Android’s Text View uses px, dp, sp, in, or mm.
On Android what should I write for:
<TextView android:textSize="?" />
Thanks!
If you check the Android Developers site it will give you the appropriate format and sizing units for TextView. You have: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), or mm (millimeters).
A font in defined in points is actually defined in physical inches. That conversion is the most straightforward, and probably the poorest way to do this, but you can convert to inches by simply multiplying the point size by the ratio of points to a physical inch: 1/72
In order to convert the point size to pixels you can make use of a calculation available at this site: https://web.archive.org/web/20120415064052/http://www.emdpi.com/screendpi.html
Some of the information there is Windows specific, but the basic method should work for Android as well, provided that you can identify your screen dpi. This method will give you the appropriate value in pixels (px), which is valid for the TextView font size.
Note, TextView recomends the use of scaled pixels (sp) for use with TextView, so you may need to do some additional work to scale your font to your screen size programmatically.