Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove excessive line spacing of downloadable fonts?

I am investigating downloadable fonts (and fonts as resources) within my current Android application.

When I place fonts within the values/font folder they display as desired.

When I employ downloadable fonts they always have additional spacing between the lines.

I have to use android:lineSpacingMultiplier="0.5" within my layout xml to achieve the desired spacing.

I am applying the fonts to a textview and allowing it to wrap content.

I am concerned that using android:lineSpacingMultiplier may "clip" some characters.

What am I doing (or not doing) to create this text spacing issue?

Here's my layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/lexie_readable_regular"
        android:padding="4dp"
        android:text="@string/testing"
        android:textColor="@color/daynight_textColor"
        android:textSize="18sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/inknut_antiqua_light"
        android:padding="4dp"
        android:text="@string/testing"
        android:textColor="@color/daynight_textColor"
        android:textSize="18sp"
        android:textStyle="normal" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/tiresias_infofont"
        android:padding="4dp"
        android:text="@string/testing"
        android:textColor="@color/daynight_textColor"
        android:textSize="18sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/aphont_regular_q15c"
        android:padding="4dp"
        android:text="@string/testing"
        android:textColor="@color/daynight_textColor"
        android:textSize="18sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="CLICK ME"
        android:textColor="@color/daynight_textColor" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:src="@drawable/placeholder" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtNightMode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:fontFamily="@font/tiresias_infofont"
            android:paddingRight="8dp"
            android:text="Night Mode"
            android:textColor="@color/daynight_textColor" />


        <android.support.v7.widget.SwitchCompat
            android:id="@+id/switchCompat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:checked="false"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:theme="@style/MySwitch" />

    </LinearLayout>

</LinearLayout>

The "downloadable" font is

android:fontFamily="@font/inknut_antiqua_light"

All the others are fonts I have manually placed in the /font folder.

And here is a screenshot of the issue:

enter image description here

This downloadable font looks OK

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
        app:fontProviderAuthority="com.google.android.gms.fonts"
        app:fontProviderPackage="com.google.android.gms"
        app:fontProviderQuery="Actor"
        app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

while this one has the issue shown above:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
        app:fontProviderAuthority="com.google.android.gms.fonts"
        app:fontProviderPackage="com.google.android.gms"
        app:fontProviderQuery="name=Inknut Antiqua"
        app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

What is the difference?

I've tried using android:includeFontPadding="false"; it does not have the desired effect.

like image 267
Hector Avatar asked Apr 05 '18 14:04

Hector


1 Answers

This is probably sinful of me but I was desperate to solve the line spacing issue, and adding a negative value to lineSpacingExtra seemed to work:

  android:lineSpacingExtra="-4sp"

For the "font padding" on the top and bottom of the TextView, you can use

  android:includeFontPadding="false"

I've tested on Android 5.1.1 & 7.0.

Fyi, I'm using sp because this is dealing with fonts.

like image 113
Aba Avatar answered Nov 15 '22 20:11

Aba