Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Support res/font by app:sb_textFont #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

28 changes: 5 additions & 23 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -16,6 +17,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Aug 07 02:27:44 EET 2017
#Thu Aug 09 14:39:02 KST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
14 changes: 7 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.ceryle'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 11
targetSdkVersion 25
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand Down Expand Up @@ -44,7 +44,7 @@ artifacts {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SegmentedButton extends View {
Expand Down Expand Up @@ -113,7 +109,9 @@ private void initText() {
mTextPaint.setTextSize(textSize);
mTextPaint.setColor(textColor);

if (hasTextTypefacePath)
if (hasTextFont)
setTextFont(textFont);
else if (hasTextTypefacePath)
setTypeface(textTypefacePath);
else if (null != textTypeface) {
setTypeface(textTypeface);
Expand Down Expand Up @@ -418,9 +416,9 @@ public void clipToRight(float clip) {
}

private int drawableTintOnSelection, textColorOnSelection, textColor, rippleColor, buttonWidth,
drawable, drawableTint, drawableWidth, drawableHeight, drawablePadding;
drawable, drawableTint, drawableWidth, drawableHeight, drawablePadding, textFont;
private boolean hasTextColorOnSelection, hasRipple, hasWidth, hasWeight, hasDrawableTintOnSelection,
hasDrawableWidth, hasDrawableHeight, hasDrawableTint, hasTextTypefacePath;
hasDrawableWidth, hasDrawableHeight, hasDrawableTint, hasTextTypefacePath, hasTextFont;
private float buttonWeight, textSize;
private String textTypefacePath, text;
private Typeface textTypeface;
Expand All @@ -441,7 +439,10 @@ private void getAttributes(AttributeSet attrs) {
hasText = ta.hasValue(R.styleable.SegmentedButton_sb_text);
textSize = ta.getDimension(R.styleable.SegmentedButton_sb_textSize, ConversionHelper.spToPx(getContext(), 14));
textColor = ta.getColor(R.styleable.SegmentedButton_sb_textColor, Color.GRAY);
textFont = ta.getResourceId(R.styleable.SegmentedButton_sb_textFont, 0);
textTypefacePath = ta.getString(R.styleable.SegmentedButton_sb_textTypefacePath);

hasTextFont = ta.hasValue(R.styleable.SegmentedButton_sb_textFont);
hasTextTypefacePath = ta.hasValue(R.styleable.SegmentedButton_sb_textTypefacePath);
int typeface = ta.getInt(R.styleable.SegmentedButton_sb_textTypeface, 1);
switch (typeface) {
Expand Down Expand Up @@ -485,7 +486,6 @@ private void getAttributes(AttributeSet attrs) {

drawableGravity = DrawableGravity.getById(ta.getInteger(R.styleable.SegmentedButton_sb_drawableGravity, 0));


ta.recycle();
}

Expand All @@ -504,13 +504,25 @@ public void setTypeface(Typeface typeface) {
/**
* @param location is .ttf file's path in assets folder. Example: 'fonts/my_font.ttf'
*/

public void setTypeface(String location) {
if (null != location && !location.equals("")) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), location);
mTextPaint.setTypeface(typeface);
}
}

/**
* @param font is R reference. Example: 'res/font/my_font.ttf' to R.font.my_font
*/

public void setTextFont(int font) {
if (0 < font) {
Typeface typeface = ResourcesCompat.getFont(context, font);
mTextPaint.setTypeface(typeface);
}
}

/**
* GRAVITY
*/
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<flag name="serif" value="3"/>
</attr>
<attr name="sb_textTypefacePath" format="string"/>
<attr name="sb_textFont" format="reference"/>
<attr name="sb_textFillSpace" format="boolean"/>
<attr name="sb_textStyle" format="integer">
<flag name="normal" value="0"/>
Expand Down
16 changes: 8 additions & 8 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
applicationId "co.ceryle.segmentedbutton.sample"
minSdkVersion 11
targetSdkVersion 25
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand All @@ -20,8 +20,8 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(":library")
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation project(":library")
}
6 changes: 3 additions & 3 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
app:sb_drawableGravity="top"
app:sb_text="Aragorn"
app:sb_textColor_onSelection="@color/orange_700"
app:sb_textTypefacePath="fonts/aniron.ttf"/>
app:sb_textFont="@font/aniron"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="0dp"
Expand All @@ -99,7 +99,7 @@
app:sb_drawableGravity="top"
app:sb_text="Gimli"
app:sb_textColor_onSelection="@color/grey_600"
app:sb_textTypefacePath="fonts/aniron.ttf"/>
app:sb_textFont="@font/aniron"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="0dp"
Expand All @@ -109,7 +109,7 @@
app:sb_drawableGravity="top"
app:sb_text="Legolas"
app:sb_textColor_onSelection="@color/yellow_200"
app:sb_textTypefacePath="fonts/aniron.ttf"/>
app:sb_textFont="@font/aniron"/>
</co.ceryle.segmentedbutton.SegmentedButtonGroup>

<co.ceryle.segmentedbutton.SegmentedButtonGroup
Expand Down