Get Font Scaling Factor to Calculate the Fontsize

Get font scaling factor to calculate the fontsize

I just found in the source code of Settings.System this function:

/** @hide */
public static void getConfigurationForUser(ContentResolver cr,
Configuration outConfig, int userHandle) {
outConfig.fontScale = Settings.System.getFloatForUser(
cr, FONT_SCALE, outConfig.fontScale, userHandle);
if (outConfig.fontScale < 0) {
outConfig.fontScale = 1;
}
}

There is however the FONT_SCALE in usage so I checked for that Configuration class where the documentation points to getResources().getConfiguration(). So I counld fix my code by using:

float scale = getResources().getConfiguration().fontScale;

Since my question was about to calculate the correct font size in pixel here is the way I use it nowerdays in Kotlin:

val Number.dpInPx: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, toFloat(), Resources.getSystem().displayMetrics).toInt()

val Number.spInPx: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, toFloat(), Resources.getSystem().displayMetrics).toInt()

The usage is:

val textSize = 42.spInPx
val padding = 8.dpInPx

Scale font size based on screen density

what you need to do is to perform a conversion using the scaleDensity (here the documentation) value, member of the DisplayMetrics
class. From the documentation

A scaling factor for fonts displayed on the display. This is the same
as density, except that it may be adjusted in smaller increments at
runtime based on a user preference for the font size.

Font scaling based on size of container

If the container is not the body, CSS Tricks covers all of your options in Fitting Text to a Container.

If the container is the body, what you are looking for is Viewport-percentage lengths:

The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly. However, when the value of overflow on the root element is auto, any scroll bars are assumed not to exist.

The values are:

  • vw (% of the viewport width)
  • vh (% of the viewport height)
  • vi (1% of the viewport size in the direction of the root element's inline axis)
  • vb (1% of the viewport size in the direction of the root element's block axis)
  • vmin (the smaller of vw or vh)
  • vmax (the larger or vw or vh)

1 v* is equal to 1% of the initial containing block.

Using it looks like this:

p {
font-size: 4vw;
}

As you can see, when the viewport width increases, so do the font-size, without needing to use media queries.

These values are a sizing unit, just like px or em, so they can be used to size other elements as well, such as width, margin, or padding.

Browser support is pretty good, but you'll likely need a fallback, such as:

p {
font-size: 16px;
font-size: 4vw;
}

Check out the support statistics: http://caniuse.com/#feat=viewport-units.

Also, check out CSS-Tricks for a broader look: Viewport Sized Typography

Here's a nice article about setting minimum/maximum sizes and exercising a bit more control over the sizes: Precise control over responsive typography

And here's an article about setting your size using calc() so that the text fills the viewport: http://codepen.io/CrocoDillon/pen/fBJxu

Also, please view this article, which uses a technique dubbed 'molten leading' to adjust the line-height as well. Molten Leading in CSS

Manually calculate font size for drawn NSAttributedString based on container width

I achieved this by finding the scale factor by diving the smaller container width with the proposed string width, then using that scale factor to determine a new font size.

var title = NSAttributedString(string: "Example", attributes: [NSAttributedString.Key.font: font.withSize(fontSize)])

let titleWidth = title.size().width
if titleWidth > containerWidth {
let scaleFactor = containerWidth / titleWidth
title = NSAttributedString(string: title.string, attributes: [NSAttributedString.Key.font: font.withSize(fontSize * scaleFactor)])
}
title.draw(in: CGRect(x: 100, y: 100, width: containerWidth, height: containerHeight))


Related Topics



Leave a reply



Submit