Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
270 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/pixplicity/fontview/sample/MainActivity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,81 @@ | ||
package com.pixplicity.fontview.sample; | ||
|
||
import android.graphics.Typeface; | ||
import android.os.Bundle; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.PopupMenu; | ||
import android.support.v7.widget.Toolbar; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.widget.Spinner; | ||
|
||
import com.pixplicity.fontview.utils.FontSpinnerAdapter; | ||
import com.pixplicity.fontview.utils.FontUtil; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
private Typeface mTypeface; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.main_activity); | ||
|
||
// Configure toolbar | ||
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
setSupportActionBar(toolbar); | ||
final ActionBar supportActionBar = getSupportActionBar(); | ||
if (supportActionBar != null) { | ||
supportActionBar.setDisplayShowTitleEnabled(false); | ||
} | ||
|
||
// Load font manually | ||
mTypeface = FontUtil.getTypeface(this, "fonts/Action_Man.ttf"); | ||
|
||
// Apply font to popup menu | ||
findViewById(R.id.bt_popup).setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
showPopupMenu(v); | ||
} | ||
}); | ||
|
||
// Apply font to Spinner | ||
final Spinner spinner = (Spinner) findViewById(R.id.spinner); | ||
spinner.setAdapter(new FontSpinnerAdapter(this, mTypeface, R.array.spinner_list)); | ||
} | ||
|
||
private void showPopupMenu(View anchorView) { | ||
PopupMenu popup = new PopupMenu(MainActivity.this, anchorView); | ||
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu()); | ||
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { | ||
public boolean onMenuItemClick(MenuItem item) { | ||
// ... | ||
return true; | ||
} | ||
}); | ||
|
||
// Apply custom font to popup menu | ||
FontUtil.applyTypeface(popup.getMenu(), mTypeface); | ||
popup.show(); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
MenuInflater inflater = getMenuInflater(); | ||
inflater.inflate(R.menu.menu, menu); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onPrepareOptionsMenu(Menu menu) { | ||
|
||
// Apply custom font to overflow menu | ||
FontUtil.applyTypeface(menu, mTypeface); | ||
return super.onPrepareOptionsMenu(menu); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item | ||
android:id="@+id/one" | ||
android:title="One"/> | ||
|
||
<item | ||
android:id="@+id/two" | ||
android:title="Two"/> | ||
|
||
</menu> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
<resources> | ||
<string name="app_name">FontView Sample</string> | ||
<string name="__leak_canary_display_activity_label">FontView Leaks Sample</string> | ||
|
||
<string-array name="spinner_list"> | ||
<item>One</item> | ||
<item>Two</item> | ||
<item>Three</item> | ||
<item>Four</item> | ||
</string-array> | ||
</resources> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Tue Jul 07 11:06:21 CEST 2015 | ||
#Mon Apr 10 16:55:25 CEST 2017 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip |
This file contains 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
44 changes: 44 additions & 0 deletions
44
library/src/main/java/com/pixplicity/fontview/utils/FontSpan.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.pixplicity.fontview.utils; | ||
|
||
import android.graphics.Paint; | ||
import android.graphics.Typeface; | ||
import android.text.TextPaint; | ||
import android.text.style.MetricAffectingSpan; | ||
|
||
/** | ||
* Span that applies a Typeface to a String | ||
*/ | ||
public class FontSpan extends MetricAffectingSpan { | ||
|
||
private final Typeface typeface; | ||
|
||
public FontSpan(final Typeface typeface) { | ||
this.typeface = typeface; | ||
} | ||
|
||
@Override | ||
public void updateDrawState(final TextPaint drawState) { | ||
apply(drawState); | ||
} | ||
|
||
@Override | ||
public void updateMeasureState(final TextPaint paint) { | ||
apply(paint); | ||
} | ||
|
||
private void apply(final Paint paint) { | ||
final Typeface oldTypeface = paint.getTypeface(); | ||
final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0; | ||
final int fakeStyle = oldStyle & ~typeface.getStyle(); | ||
|
||
if ((fakeStyle & Typeface.BOLD) != 0) { | ||
paint.setFakeBoldText(true); | ||
} | ||
|
||
if ((fakeStyle & Typeface.ITALIC) != 0) { | ||
paint.setTextSkewX(-0.25f); | ||
} | ||
|
||
paint.setTypeface(typeface); | ||
} | ||
} |
Oops, something went wrong.