Android Open Source - android_font_widgets Font Applier






From Project

Back to project page android_font_widgets.

License

The source code is released under:

[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...

If you think the Android project android_font_widgets listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * =================================================================================================
 *                 Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License
 * you may obtain at// www . j  ava  2  s.  c o  m
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * You can redistribute, modify or publish any part of the code written within this file but as it
 * is described in the License, the software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.ui.widget.font;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * <h3>Class Overview</h3>
 * Helper to simplify setting of the custom fonts to FontWidgets.
 *
 * @author Martin Albedinsky
 */
public final class FontApplier {

  /**
   * Interface ===================================================================================
   */

  /**
   * <h3>Class Overview</h3>
   * Base required implementation for a widget which want to use {@link FontApplier} api to set up
   * custom font.
   *
   * @author Martin Albedinsky
   */
  public static interface FontWidget {

    /**
     */
    public void setTypeface(@Nullable Typeface tf);

    /**
     */
    @NonNull
    public Context getContext();

    /**
     * Wrapped {@link FontApplier#applyFont(int)}.
     *
     * @see #setFont(String, int)
     * @see #setFont(Font)
     */
    public void setFont(@StyleRes int style);

    /**
     * Wrapped {@link #setFont(String, int)} with {@link android.graphics.Typeface#NORMAL} flag
     * for <var>textStyle</var>.
     */
    public void setFont(@NonNull String fontPath);

    /**
     * Wrapped {@link FontApplier#applyFont(String, int)}.
     *
     * @see #setFont(int)
     * @see #setFont(Font)
     */
    public void setFont(@NonNull String fontPath, @TypefaceStyle.TextStyle int textStyle);

    /**
     * Wrapped {@link FontApplier#applyFont(Font)}.
     *
     * @see #setFont(int)
     * @see #setFont(String, int)
     */
    public void setFont(@Nullable Font font);
  }

  /**
   * Constants ===================================================================================
   */

  /**
   * Log TAG.
   */
  // private static final String TAG = "FontApplier";

  /**
   * Flag indicating whether the debug output trough log-cat is enabled or not.
   */
  // private static final boolean DEBUG_ENABLED = true;

  /**
   * Flag indicating whether the output trough log-cat is enabled or not.
   */
  // private static final boolean LOG_ENABLED = true;

  /**
   * Static members ==============================================================================
   */

  /**
   * Members =====================================================================================
   */

  /**
   * An instance of font widget to which apply custom font typefaces.
   */
  final FontWidget mFontWidget;

  /**
   * Current valid context obtained from the font widget.
   */
  private final Context mContext;

  /**
   * Constructors ================================================================================
   */

  /**
   * Creates a new instance of FontApplier for the given <var>fontWidget</var>.
   *
   * @param fontWidget An instance of widget to which will be applied all font typefaces requested
   *                   trough this font applier.
   */
  public FontApplier(@NonNull FontWidget fontWidget) {
    this.mFontWidget = fontWidget;
    this.mContext = fontWidget.getContext();
  }

  /**
   * Methods =====================================================================================
   */

  /**
   * Public --------------------------------------------------------------------------------------
   */

  /**
   * Same as {@link #applyFont(android.util.AttributeSet, int)} with {@code 0} as attribute for
   * <var>defStyle</var>.
   */
  public boolean applyFont(@NonNull AttributeSet attrs) {
    return applyFont(attrs, 0);
  }

  /**
   * Creates and applies font from the passed <var>attrs</var> to the current font widget.
   * See {@link Font#create(android.content.Context, android.util.AttributeSet, int)} for more info.
   *
   * @return {@code True} if font was applied, {@code false} otherwise.
   */
  public boolean applyFont(@NonNull AttributeSet attrs, @AttrRes int defStyle) {
    return applyFont(Font.create(mContext, attrs, defStyle));
  }

  /**
   * Creates and applies font from the passed <var>style</var> to the current font widget.
   * See {@link Font#create(android.content.Context, int)} for more info.
   *
   * @return {@code True} if font was applied, {@code false} otherwise.
   */
  public boolean applyFont(@StyleRes int style) {
    return applyFont(Font.create(mContext, style));
  }

  /**
   * Creates and applies font from the passed <var>fontPath</var> to the current font widget.
   * See {@link Font#create(String, int)} for more info.
   *
   * @return {@code True} if font was applied, {@code false} otherwise.
   */
  public boolean applyFont(@NonNull String fontPath, @TypefaceStyle.TextStyle int textStyle) {
    return applyFont(Font.create(fontPath, textStyle));
  }

  /**
   * Applies the given <var>font</var> to the current font widget.
   *
   * @param font An instance of valid font to apply.
   * @return {@code True} if font was applied, {@code false} otherwise.
   */
  public boolean applyFont(@Nullable Font font) {
    return applyFontInternal(font);
  }

  /**
   * Getters + Setters ---------------------------------------------------------------------------
   */

  /**
   * Returns the font widget of this font applier.
   *
   * @return An instance of the font widget for which was this font applier instance created.
   */
  @NonNull
  public FontWidget getFontWidget() {
    return mFontWidget;
  }

  /**
   * Protected -----------------------------------------------------------------------------------
   */

  /**
   * Private -------------------------------------------------------------------------------------
   */

  /**
   * Applies the given font's type face to the current font view.
   *
   * @param font Font which type face to apply.
   * @return {@code True} if font was applied, {@code false} otherwise.
   */
  private boolean applyFontInternal(Font font) {
    if (font != null) {
      mFontWidget.setTypeface(font.obtainTypeface(mContext));
      return true;
    }
    return false;
  }

  /**
   * Inner classes ===============================================================================
   */

  /**
   * <h3>Class Overview</h3>
   * Wrapper implementation of {@link FontWidget} to allow applying of custom font to widgets not
   * from within this library.
   *
   * @author Martin Albedinsky
   */
  public static final class FontWidgetWrapper implements FontWidget {

    /**
     * Members =================================================================================
     */

    /**
     * Wrapped font view.
     */
    private final TextView fontView;

    /**
     * Context obtained from the wrapped font view.
     */
    private final Context context;

    /**
     * Constructors ============================================================================
     */

    /**
     * Creates a new instance of FontWidgetWrapper to wrap the given <var>fontView</var>.
     *
     * @param fontView Font view to wrap.
     */
    public FontWidgetWrapper(@NonNull TextView fontView) {
      this.fontView = fontView;
      this.context = fontView.getContext();
    }

    /**
     * Methods =================================================================================
     */

    /**
     */
    @Override
    public void setTypeface(@Nullable Typeface tf) {
      fontView.setTypeface(tf);
    }

    /**
     */
    @NonNull
    @Override
    public Context getContext() {
      return fontView.getContext();
    }

    /**
     */
    @Override
    public void setFont(@StyleRes int style) {
      this.setFontInner(Font.create(context, style));
    }

    /**
     */
    @Override
    public void setFont(@NonNull String fontPath) {
      this.setFontInner(Font.create(fontPath));
    }

    /**
     */
    @Override
    public void setFont(@NonNull String fontPath, @TypefaceStyle.TextStyle int textStyle) {
      this.setFontInner(Font.create(fontPath, textStyle));
    }

    /**
     */
    @Override
    public void setFont(@Nullable Font font) {
      this.setFontInner(font);
    }

    /**
     * Sets the given <var>font</var> (if not {@code null}) to the wrapped font view.
     *
     * @param font Font to apply.
     */
    private void setFontInner(Font font) {
      if (font != null) {
        fontView.setTypeface(font.obtainTypeface(context));
      }
    }
  }
}




Java Source Code List

com.wit.android.ui.widget.font.FontApplier.java
com.wit.android.ui.widget.font.FontAutoCompleteTextView.java
com.wit.android.ui.widget.font.FontButton.java
com.wit.android.ui.widget.font.FontCheckBox.java
com.wit.android.ui.widget.font.FontEditText.java
com.wit.android.ui.widget.font.FontRadioButton.java
com.wit.android.ui.widget.font.FontTextView.java
com.wit.android.ui.widget.font.FontWidgetsConfig.java
com.wit.android.ui.widget.font.Font.java
com.wit.android.ui.widget.font.TypefaceStyle.java