Android Open Source - android_font_widgets Font Text View






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) 2012 - 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/*from w  ww.  j ava  2 s.  c  om*/
 *
 *     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.content.res.Resources;
import android.content.res.TypedArray;
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>
 * Updated implementation of {@link android.widget.TextView TextView} widget to simplify using of custom
 * fonts.
 * <h3>Default Font set up:</h3>
 * <ul>
 * <li>
 * <b>TextAppearance:</b>
 * <p>
 * An {@link R.styleable#Ui_Widget_uiFontPath ui:uiFontPath} attribute can be placed within a
 * <b>text appearance</b> style. This is a good place to define a global font for all font widgets.
 * Also each font widget can have its own text appearance style defined to set different fonts to
 * different font widgets.
 * <p>
 * When a new instance of this FontWidget implementation is being created, text appearance style is
 * checked as first for <b>fontPath</b>.
 * </li>
 * <li>
 * <b>Style:</b>
 * <p>
 * An {@link R.styleable#Ui_Widget_uiFontPath ui:uiFontPath} attribute can be also placed directly
 * within the specific style set to this FontWidget implementation within XML layout.
 * <p>
 * In this case, <b>fontPath</b> will be looked up in AttributeSet passed to constructor.
 * </li>
 * </ul>
 *
 * @attr ref R.styleable#Ui_Widget_uiFontPath
 *
 * @author Martin Albedinsky
 */
public class FontTextView extends TextView implements FontApplier.FontWidget {

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

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

  /**
   * 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;

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

  /**
   * Helper to apply custom font to this view.
   */
  private final FontApplier FONT_APPLIER = new FontApplier(this);

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

  /**
   * Same as {@link #FontTextView(android.content.Context, android.util.AttributeSet)}
   * without attributes.
   */
  public FontTextView(@NonNull Context context) {
    this(context, null);
  }

  /**
   * Same as {@link #FontTextView(android.content.Context, android.util.AttributeSet, int)}
   * with {@link android.R.attr#textViewStyle} as attribute for default style.
   */
  public FontTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, android.R.attr.textViewStyle);
  }

  /**
   * Creates a new instance of FontTextView like {@link android.widget.TextView#TextView(android.content.Context, android.util.AttributeSet, int)}.
   * <p>
   * If theme of the given <var>context</var> or the given <var>attrs</var> contains attribute
   * {@link R.styleable#Ui_Widget_uiFontPath ui:uiFontPath}, path obtained from this attribute will
   * be used to set up {@link Font} for this font view using instance of {@link FontApplier}.
   */
  public FontTextView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode() && attrs != null) {
      final Resources.Theme theme = context.getTheme();
      // Get font path presented within text appearance style.
      if (theme != null) {
        final TypedArray appearanceArray = theme.obtainStyledAttributes(attrs, new int[]{android.R.attr.textAppearance}, defStyle, 0);
        if (appearanceArray != null) {
          final int appearance = appearanceArray.getResourceId(0, -1);
          if (appearance != -1) {
            setFont(appearance);
          }
          appearanceArray.recycle();
        }
      }
      // Get font path presented within xml attributes.
      FONT_APPLIER.applyFont(attrs, defStyle);
    }
  }

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

  /**
   */
  @Override
  public void setFont(@StyleRes int style) {
    FONT_APPLIER.applyFont(style);
  }

  /**
   */
  @Override
  public void setFont(@NonNull String fontPath) {
    setFont(fontPath, Typeface.NORMAL);
  }

  /**
   */
  @Override
  public void setFont(@NonNull String fontPath, @TypefaceStyle.TextStyle int textStyle) {
    FONT_APPLIER.applyFont(fontPath, textStyle);
  }

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

  /**
   */
  @Override
  public void setTextAppearance(@NonNull Context context, @StyleRes int resId) {
    super.setTextAppearance(context, resId);
    setFont(resId);
  }
}




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