How "Android" Changes TextView font spacing

Source: Internet
Author: User
Tags gettext

Method:

1. Custom fonts, where you add spacing, more complex
2. Space between letters, not desirable, the width is not flexible
3. Use Textscalex and space, custom TextView, let Textscalex only change the spacing, do not change the font
4. Carbon OnDraw, each time you draw a character, manually control the position of each character.

The code for Method 3 is provided below, with a reference to the link:

Https://stackoverflow.com/questions/14283246/change-text-kerning-or-spacing-in-textview

Package Nl.raakict.android.spc.widget;import Android.content.context;import Android.text.spannable;import Android.text.spannablestring;import Android.text.style.scalexspan;import Android.util.attributeset;import Android.widget.textview;public class Letterspacingtextview extends TextView {private float letterspacing = Letterspaci Ng.    Biggest;    Private Charsequence Originaltext = "";    Public Letterspacingtextview (Context context) {super (context);        } public Letterspacingtextview (context context, AttributeSet Attrs) {Super (context, attrs);        Originaltext = Super.gettext ();        Applyletterspacing ();    This.invalidate (); } public Letterspacingtextview (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, defstyl    e);    } public float getletterspacing () {return letterspacing;        } public void setletterspacing (float letterspacing) {this.letterspacing = letterspacing;    Applyletterspacing ();  }  @Override public void SetText (charsequence text, Buffertype type) {originaltext = text;    Applyletterspacing ();    } @Override Public Charsequence GetText () {return originaltext;        } private void Applyletterspacing () {StringBuilder builder = new StringBuilder ();            for (int i = 0; i < originaltext.length (); i++) {String c = "" + Originaltext.charat (i);            Builder.append (C.tolowercase ());            if (I+1 < Originaltext.length ()) {builder.append ("\u00a0");        }} spannablestring Finaltext = new Spannablestring (builder.tostring ());                if (builder.tostring (). Length () > 1) {for (int i = 1; i < builder.tostring (). Length (); i+=2) {            Finaltext.setspan (New Scalexspan ((letterspacing+1)/10), I, i+1, spannable.span_exclusive_exclusive);    }} super.settext (Finaltext, buffertype.spannable);      } public class Letterspacing {  Public final static float NORMAL = 0;        Public final static Float Normalbig = (float) 0.025;        Public final static Float BIG = (float) 0.05;    Public final static Float biggest = (float) 0.2; }}

  

How "Android" Changes TextView font spacing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.