-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from TinkoffCreditSystems/master
Added ability to set typefaces for custom views with setTypeface method
- Loading branch information
Showing
8 changed files
with
195 additions
and
15 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
75 changes: 75 additions & 0 deletions
75
...ample/src/main/java/uk/co/chrisjenx/calligraphy/sample/CustomViewWithTypefaceSupport.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,75 @@ | ||
package uk.co.chrisjenx.calligraphy.sample; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.graphics.Rect; | ||
import android.graphics.Typeface; | ||
import android.os.Build; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
/** | ||
* @author Dmitriy Tarasov | ||
*/ | ||
public class CustomViewWithTypefaceSupport extends View { | ||
|
||
private Paint paint; | ||
private Rect textBounds; | ||
private int width; | ||
private int height; | ||
|
||
public CustomViewWithTypefaceSupport(Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
public CustomViewWithTypefaceSupport(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
public CustomViewWithTypefaceSupport(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
public CustomViewWithTypefaceSupport(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
paint = new Paint(); | ||
paint.setTextSize(50); | ||
textBounds = new Rect(); | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
String text = "This is a custom view with setTypeface support"; | ||
Paint.FontMetrics fm = paint.getFontMetrics(); | ||
paint.getTextBounds(text, 0, text.length(), textBounds); | ||
|
||
width = textBounds.left + textBounds.right + getPaddingLeft() + getPaddingRight(); | ||
height = (int) (Math.abs(fm.top) + fm.bottom); | ||
|
||
canvas.drawText(text, 0, -fm.top + getPaddingTop(), paint); | ||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
setMeasuredDimension(width, height); | ||
} | ||
|
||
/** | ||
* Used by Calligraphy to change view's typeface | ||
*/ | ||
@SuppressWarnings("unused") | ||
public void setTypeface(Typeface tf) { | ||
paint.setTypeface(tf); | ||
invalidate(); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/HasTypeface.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,22 @@ | ||
package uk.co.chrisjenx.calligraphy; | ||
|
||
import android.graphics.Typeface; | ||
|
||
/** | ||
* There are two ways to set typeface for custom views: | ||
* <ul> | ||
* <li>Implementing this interface. You should only implements {@link #setTypeface(Typeface)} method.</li> | ||
* <li>Or via reflection. If custom view already has setTypeface method you can | ||
* register it during Calligraphy configuration | ||
* {@link uk.co.chrisjenx.calligraphy.CalligraphyConfig.Builder#addCustomViewWithSetTypeface(Class)}</li> | ||
* </ul> | ||
* First way is faster but encourage more effort from the developer to implements interface. Second one | ||
* requires less effort but works slowly cause reflection calls. | ||
* | ||
* @author Dmitriy Tarasov | ||
*/ | ||
public interface HasTypeface { | ||
|
||
void setTypeface(Typeface typeface); | ||
|
||
} |
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