Last active
November 13, 2017 19:41
-
-
Save lawloretienne/f2e019c39203f2b0a0c247ae7ac3c56a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<!-- character spacing applied to this TextView --> | |
<com.sampleapp.CustomTextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:characterSpacing="@dimen/default_character_spacing"/> | |
<!-- line spacing applied to this TextView --> | |
<com.sampleapp.CustomTextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
app:lineSpacing="@dimen/default_line_spacing"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="CustomTextView"> | |
<attr name="characterSpacing" format="float"/> | |
<attr name="lineSpacing" format="dimension"/> | |
</declare-styleable> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomTextView extends AppCompatTextView { | |
public CustomTextView(Context context) { | |
super(context); | |
init(context,null); | |
} | |
public CustomTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public CustomTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs){ | |
if (!isInEditMode()) { | |
TypedArray attributeArray = context.obtainStyledAttributes( | |
attrs, | |
R.styleable.TextView); | |
float characterSpacing; | |
float lineSpacing; | |
try { | |
characterSpacing = attributeArray.getFloat(R.styleable.CustomTextView_characterSpacing, 0.0F); | |
lineSpacing = attributeArray.getDimension(R.styleable.CustomTextView_lineSpacing, 0); | |
} finally { | |
attributeArray.recycle(); | |
} | |
setUpCharacterSpacing(characterSpacing); | |
setUpLineSpacing(lineSpacing); | |
} | |
} | |
private void setUpLineSpacing(float lineSpacing){ | |
if(lineSpacing != 0) | |
setLineSpacing(lineSpacing - getTextSize(), 1); | |
} | |
private void setUpCharacterSpacing(float characterSpacing){ | |
if(characterSpacing != 0.0F){ | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { | |
setLetterSpacing(characterSpacing / (getTextSize() / getResources().getDisplayMetrics().scaledDensity)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- These are the values from the Sketch file --> | |
<item name="default_character_spacing" format="float" type="dimen">2.0</item> | |
<dimen name="default_line_spacing">40dp</dimen> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will handle character spacing and line spacing of text in a Sketch file.