I'm developing Unity Android/iOS application. I need to show text with size relative to screen size and density. Material design guide says I should set size of my regular text to 14sp. How can I provide it?
What does actually fontSize mean in Text component? Is this number dependent on something? Unity Documentation description of fontSize says nothing about that.
Thanks for response.
So... Font Size is essentially "defined" in three places in Unity.
The first is on the font asset itself (the import settings). As far as I've been able to determine lately (post-Unity 4.5), this value is not used (I've tweaked it to insane values and had no affect on what is displayed on-screen; I know in Unity 3.5 this affected the final rendering of the font and I had to scale it up really big to get nice, cleanly rendered text, as it was effectively the "maximum font size available" and font sizes larger than this value (see #2) would get blurry).
The second is on the text component, called Font Size which does what it says. It's like picking a larger font size in any application. This is what the material design guide is referring to. Text component font sizes in Unity are measured in "pixels of height" and are 1:1 with screen pixels (except when the transform--or any of its parent transforms--are scaled, or if the canvas itself is in Worldspace (in which case you kinda have to guess)).
The third place is on the text object's Transform: you can additionally scale the object and get the (apparent) font size to change although scaling it up will make it look blurry and scaling it down will make it look aliased. Either way it looks bad.
All that said, I've found best results to be to double the font size and reduce the transform scale to 0.5 as the text anti-aliasing at the default scale looks blurry to me, so using a bigger font size and scaling the object transform back down sharpens it up. You'll still want to test for yourself if it needs it.
Related
When specifying text size on Android, the suggested the best practice is to use sp font units instead of dp, since with sp the text will increase/decrease in size according to the user's display preferences. For example, on my device I can go to
Settings -> Display -> Font
and choose a preference: small, normal, large, huge (these seem to vary across devices though).
Does anyone know a way to preview the effect of this preference in Android Studio's layout preview? It would be easier than having to deploy to a device/emulator in order to test. I can't see an option for this, but maybe I'm missing it.
Related: is there a standard multiplier for what the largest ("huge") font preference equals to? Or, can this even vary across devices? If there's no upper bound to how large the text could be, the first part of my question is probably moot.
As of about May 2020, This can now be done via the Layout Validation feature of Android Studio 4.0. This allows you to view different Font Sizes, devices and even see your layouts from the perspective of someone with color blindness.
No, there is no such way to preview the font scaling in the android studio as of now. but you can check out the material design for text scaling at < https://material.io/design/typography/the-type-system.html#type-scale > here you will some idea of how your text looks in runtime
Ok I know that there are already many questions that ask the same thing as what I am asking, however the problem is that none of those solutions have worked for me or are what I want. My problem is that when I use the setTextSize method of the Paint class, the text appears to be too big on smaller screens, and smaller on bigger screens. What I want is for me to be able to change it to the size I want on my device, and then on any other device the size should change accordingly (scale down, scale up) and it should appear the same way (NOT the same size) on other devices. And I have already tried multiplying the density by the dp I want and all that other stuff but it doesn't work.
First off, any size you use should be based off of sp- scaled pixels. Units in sp scale with respect to the user defined default size. This is to people who are hard of seeing can choose a larger text size, and all text will scale relative to this choice. You should not be designing text in either dp or in px, as neither of those will scale with the system font size. When you call getTextSize/setTextSize it should be treated as sp.
As for it looking the exact same on all screens- you're not going to get that, because different OEMs use different default heights and different default fonts, not to mention the user can override either of those. Pixel perfect should not be your goal. Which is good, because it isn't actually possible.
I am writing an app about eye test. It is necessary to set the standard text size. I used the following code but it showed what I did not expect.
Typeface type=Typeface.createFromAsset(getAssets(),"Optotypes.ttf");
textView2.setTypeface(type);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_MM,25);
textView2.setText(randomLetter);
I expected the textview show a 2.5cm letter but it is not the exact length/height still.
This situation appear also on different device.
The next problem is that the size is different between the original font and ttf I added. (the original font didn't show the text with 2.5cm also.
Is my code wrong or anything else i missed ? Thanks guys . it is important to me.
I think you're missing how Android handles text sizes.
In Android, you should specify text size in SP units, so Android can scale it accordingly to the user's font size preferences. Never specify hardcoded pixels or centimeters.
Check this references for documentation on the subject:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you want to set the text size in SP programatically, you can do this
// same as android:textSize="15sp" in XML
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
-- EDIT
Keep in mind that by just setting a certain text size it doesn't mean that every letter will be of that size. Remember that there are multiple letters with multiple sizes. For instance, with a size of 20mm, this is what you get
Because Android needs to accommodate every possible character in a textview with the size you provided. That being said, textSize is not 100% accurate to what you provide to it.
If this is not enough for you, please provide more details of the problem you have at hands.
So basically I have 3 boxes on screen, each displaying different information regarding audio frequencys.
The top box shows the Note, Middle shows the pitch of that Note, and 3rd shows your pitch.
I wanted to have the numbers in a box, and since they constantly change, I wanted the box to remain a certain size instead of constantly increasing in size to fit the text. However, I also don't want to give the boxes a specific size as I want them to be able to resize depending on the screen size.
Is there a way to do this easily? Or would I have to create separate XML files for each device or something?
I will attach an image so you can see what I mean.
Image: http://i.imgur.com/5NVR54N.png
So just imagine that each box is resizing itself depending on what text is placed in the box.
Any help with this would be great thanks!
If "So just imagine that each box is re-sizing itself depending on what text is placed in the box" this is needed then in the XML for the TextView widget that you use for displaying the values use.,
android:layoutWidth="wrap_content", android:layoutHeight="wrap_content" . The wXh of the view would be adjusted according to the content (values) .
If "I wanted the box to remain a certain size instead of constantly increasing in size to fit the text." -If this is not done, that is if you have fixed width text widgets, then the text will be clipped if they exceed the max width.
2.1 By above if you mean same size on each device, then figure out the maximum possible width/height on a mdpi (320X480 mdpi) emulator for each textview, say 100px width and 80px height and is what suits you for the Note, then in the XML for the TextView widget for Note use android:layoutWidth="100dp", android:layoutHeight="80dp" where "dp" would give you device independent pixel size, meaning the size will be adjusted according to the screen density on which the emulator is running.
http://developer.android.com/training/multiscreen/screensizes.html and related material should help.
I have an iOS app that uses unique textured backgrounds (think of a game title screen or something) on each of the screens that I am trying to port to Android. On iOS they knew the resolutions and just designed the backgrounds around those. Obviously that isn't possible on Android.
What is the best way to put a textured/non-repeatable background into Android that works on various screen sizes and aspect ratios?
The solutions I have thought of so far are:
Fit the shortest dimension and allow the image to go off the edge of the screen for the larger dimension
Make the background image large and center it, then make it stretch out beyond the edges when the resolution is smaller than the image
Stretch the image to just fit (this is ugly and I'd like to avoid it)
Create a different version of the image for each resolution. (This seems way beyond the scope of what is possible for this project.)
As far as I can tell in Android, 1. and 2. aren't possible out of the box because background images set the size of the view to match their size (you can't tell it to just extend beyond the edges, please correct me if I am wrong).
What solutions would you use in this situation?
Use an (match_parent) ImageView to simulate back ground image. (By adjusting the scale type of imageview you can get the result of point 1,2)
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Use 9-patch image. You can specify which part of your image can be stretched. For your case I suggest adding a small "margin" to your image, and make it stretchable using 9-patch. Only the margin will be stretched, maintaining the aspect ratio of your image.
android developer 9-patch
Create a different version of the image for each resolution.
As you're probably aware, Android can run on a wide variety of devices with varying resolutions and screen densities. To be fully compatible with all of them takes a bit of graphics work; you need resources for each possible screen density (and in this case, resolution).
For full details, see Supporting Multiple Screens in the Android developer documentation.
Unless something has gone seriously wrong in your development process, you should have the original files (e.g. .psd) to work with when creating these resources.