Java Code Examples for android.graphics.Typeface#BOLD_ITALIC

The following examples show how to use android.graphics.Typeface#BOLD_ITALIC . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KnifeText.java    From Knife with Apache License 2.0 6 votes vote down vote up
protected void styleValid(int style, int start, int end) {
    switch (style) {
        case Typeface.NORMAL:
        case Typeface.BOLD:
        case Typeface.ITALIC:
        case Typeface.BOLD_ITALIC:
            break;
        default:
            return;
    }

    if (start >= end) {
        return;
    }

    getEditableText().setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
 
Example 2
Source File: FontAssetManager.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
private Typeface typefaceForStyle(Typeface typeface, String style) {
  int styleInt = Typeface.NORMAL;
  boolean containsItalic = style.contains("Italic");
  boolean containsBold = style.contains("Bold");
  if (containsItalic && containsBold) {
    styleInt = Typeface.BOLD_ITALIC;
  } else if (containsItalic) {
    styleInt = Typeface.ITALIC;
  } else if (containsBold) {
    styleInt = Typeface.BOLD;
  }

  if (typeface.getStyle() == styleInt) {
    return typeface;
  }

  return Typeface.create(typeface, styleInt);
}
 
Example 3
Source File: PXFontRegistry.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
public static Typeface getTypeface(String family, String weight, String style) {
    String key = deriveKey(family, weight, style);
    Typeface match = FONT_BY_KEY.get(key);
    Typeface result;

    if (match != null) {
        result = match;

    } else {
        int typefaceStyle = Typeface.NORMAL; // default

        if ("bold".equals(weight) && "italic".equals(style)) {
            typefaceStyle = Typeface.BOLD_ITALIC;
        } else if ("bold".equals(weight)) {
            typefaceStyle = Typeface.BOLD;
        } else if ("italic".equals(style)) {
            typefaceStyle = Typeface.ITALIC;
        }

        result = Typeface.create(family, typefaceStyle);
        FONT_BY_KEY.put(key, result);
    }

    return result;

}
 
Example 4
Source File: SVGAndroidRenderer.java    From XDroidAnimation with Apache License 2.0 6 votes vote down vote up
private Typeface  checkGenericFont(String fontName, Integer fontWeight, FontStyle fontStyle)
{
   Typeface font = null;
   int      typefaceStyle;

   boolean  italic = (fontStyle == Style.FontStyle.Italic);
   typefaceStyle = (fontWeight > 500) ? (italic ? Typeface.BOLD_ITALIC : Typeface.BOLD)
                                      : (italic ? Typeface.ITALIC : Typeface.NORMAL);

   if (fontName.equals("serif")) {
      font = Typeface.create(Typeface.SERIF, typefaceStyle);
   } else if (fontName.equals("sans-serif")) {
      font = Typeface.create(Typeface.SANS_SERIF, typefaceStyle);
   } else if (fontName.equals("monospace")) {
      font = Typeface.create(Typeface.MONOSPACE, typefaceStyle);
   } else if (fontName.equals("cursive")) {
      font = Typeface.create(Typeface.SANS_SERIF, typefaceStyle);
   } else if (fontName.equals("fantasy")) {
      font = Typeface.create(Typeface.SANS_SERIF, typefaceStyle);
   }
   return font;
}
 
Example 5
Source File: FontUtils.java    From zom-android-matrix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets roboto typeface according to passed typeface style settings.
 * <p/>
 * Will get Roboto-Bold for Typeface.BOLD etc
 */
private static Typeface getRobotoTypeface(Context context, Typeface originalTypeface) {
    FontType robotoFontType = null;

    if (originalTypeface == null) {
        robotoFontType = FontType.NORMAL;
    } else {
        int style = originalTypeface.getStyle();

        switch (style) {
            case Typeface.BOLD:
                robotoFontType = FontType.BOLD;
                break;

            case Typeface.BOLD_ITALIC:
                robotoFontType = FontType.BOLD_ITALIC;
                break;

            case Typeface.ITALIC:
                robotoFontType = FontType.ITALIC;
                break;

            default:
                robotoFontType = FontType.NORMAL;
                break;

        }
    }

    return getRobotoTypeface(context, robotoFontType);
}
 
Example 6
Source File: FromTextView.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public void setText(Recipients recipients, boolean read) {
  int        attributes[]   = new int[]{R.attr.conversation_list_item_count_color};
  TypedArray colors         = getContext().obtainStyledAttributes(attributes);
  boolean    isUnnamedGroup = recipients.isGroupRecipient() && TextUtils.isEmpty(recipients.getPrimaryRecipient().getName());

  String fromString;

  if (isUnnamedGroup) {
    fromString = getContext().getString(R.string.ConversationActivity_unnamed_group);
  } else {
    fromString = recipients.toShortString();
  }

  int typeface;

  if (isUnnamedGroup) {
    if (!read) typeface = Typeface.BOLD_ITALIC;
    else       typeface = Typeface.ITALIC;
  } else if (!read) {
    typeface = Typeface.BOLD;
  } else {
    typeface = Typeface.NORMAL;
  }

  SpannableStringBuilder builder = new SpannableStringBuilder(fromString);
  builder.setSpan(new StyleSpan(typeface), 0, builder.length(),
                  Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

  colors.recycle();

  setText(builder);

  if      (recipients.isBlocked()) setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_block_grey600_18dp, 0, 0, 0);
  else if (recipients.isMuted())   setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_volume_off_grey600_18dp, 0, 0, 0);
  else                             setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
 
Example 7
Source File: FontUtils.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets roboto typeface according to passed typeface style settings.
 * <p/>
 * Will get Roboto-Bold for Typeface.BOLD etc
 */
private static Typeface getRobotoTypeface(Context context, Typeface originalTypeface) {
    FontType robotoFontType = null;

    if (originalTypeface == null) {
        robotoFontType = FontType.NORMAL;
    } else {
        int style = originalTypeface.getStyle();

        switch (style) {
            case Typeface.BOLD:
                robotoFontType = FontType.BOLD;
                break;

            case Typeface.BOLD_ITALIC:
                robotoFontType = FontType.BOLD_ITALIC;
                break;

            case Typeface.ITALIC:
                robotoFontType = FontType.ITALIC;
                break;

            default:
                robotoFontType = FontType.NORMAL;
                break;

        }
    }

    return getRobotoTypeface(context, robotoFontType);
}
 
Example 8
Source File: TextViewStyleProfile.java    From SublimeNavigationView with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the typeface style to the given value. The typeface style
 * defaults to Typeface.NORMAL if the value isn't one of
 * Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC.
 *
 * @param typefaceStyle The style to set.
 * @return This {@link TextViewStyleProfile} for chaining.
 */
public TextViewStyleProfile setTypefaceStyle(int typefaceStyle) {
    if (typefaceStyle < Typeface.NORMAL || typefaceStyle > Typeface.BOLD_ITALIC) {
        Log.e(TAG, "'setTypefaceStyle(int)' was called with a invalid value " +
                "- allowed values are Typeface.NORMAL, Typeface.BOLD, " +
                "Typeface.ITALIC and Typeface.BOLD_ITALIC.");
        mTypefaceStyle = Typeface.NORMAL;
    } else {
        mTypefaceStyle = typefaceStyle;
    }

    return this;
}
 
Example 9
Source File: TickerView.java    From ticker with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the typeface size used by this view.
 *
 * @param typeface the typeface to use on the text.
 */
public void setTypeface(Typeface typeface) {
    if (textStyle == Typeface.BOLD_ITALIC) {
        typeface = Typeface.create(typeface, Typeface.BOLD_ITALIC);
    } else if (textStyle == Typeface.BOLD) {
        typeface = Typeface.create(typeface, Typeface.BOLD);
    } else if (textStyle == Typeface.ITALIC) {
        typeface = Typeface.create(typeface, Typeface.ITALIC);
    }

    textPaint.setTypeface(typeface);
    onTextPaintMeasurementChanged();
}
 
Example 10
Source File: TextViewStyleProfile.java    From SublimeNavigationView with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the typeface style to the given value. The typeface style
 * defaults to Typeface.NORMAL if the value isn't one of
 * Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC.
 *
 * @param typefaceStyle The style to set.
 * @return This {@link TextViewStyleProfile} for chaining.
 */
public TextViewStyleProfile setTypefaceStyle(int typefaceStyle) {
    if (typefaceStyle < Typeface.NORMAL || typefaceStyle > Typeface.BOLD_ITALIC) {
        Log.e(TAG, "'setTypefaceStyle(int)' was called with a invalid value " +
                "- allowed values are Typeface.NORMAL, Typeface.BOLD, " +
                "Typeface.ITALIC and Typeface.BOLD_ITALIC.");
        mTypefaceStyle = Typeface.NORMAL;
    } else {
        mTypefaceStyle = typefaceStyle;
    }

    return this;
}
 
Example 11
Source File: SpannableActivity.java    From AndroidDemo with Apache License 2.0 5 votes vote down vote up
@Override
public void initDate() {
	String sp = "12345";
	SpannableString spannableString = new SpannableString(sp);
	StyleSpan styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);
	spannableString.setSpan(styleSpan, 0, sp.length(),
			Spannable.SPAN_INCLUSIVE_INCLUSIVE);
	tv.setText(spannableString);
	tv.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example 12
Source File: TypefaceHelper.java    From android-typeface-helper with Apache License 2.0 5 votes vote down vote up
/**
 * Check if typeface style int is one of:
 * <ul>
 *      <li>{@link android.graphics.Typeface#NORMAL}</li>
 *      <li>{@link android.graphics.Typeface#BOLD}</li>
 *      <li>{@link android.graphics.Typeface#ITALIC}</li>
 *      <li>{@link android.graphics.Typeface#BOLD_ITALIC}</li>
 * </ul>
 * @param style
 */
private static void checkTypefaceStyleThrowing(int style) {
	switch (style) {
	case Typeface.NORMAL:
	case Typeface.BOLD:
	case Typeface.ITALIC:
	case Typeface.BOLD_ITALIC:
		break;
	default:
		throw new IllegalArgumentException("Style have to be in (Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC)");
	}
}
 
Example 13
Source File: TypefaceUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * create typeface by name or path
 *
 * @param fontName
 * @return
 */
 static Typeface createByName( String fontName) {
    try {
         Typeface typeface = Typeface.create(fontName, Typeface.BOLD_ITALIC);
        if (typeface != null && Typeface.BOLD_ITALIC == typeface.getStyle()) {//得到的是默认字体则返回null
            return null;
        }
        return typeface;
    } catch (Exception e) {
        return null;
    }
}
 
Example 14
Source File: KnifeText.java    From Knife with Apache License 2.0 5 votes vote down vote up
protected boolean containStyle(int style, int start, int end) {
    switch (style) {
        case Typeface.NORMAL:
        case Typeface.BOLD:
        case Typeface.ITALIC:
        case Typeface.BOLD_ITALIC:
            break;
        default:
            return false;
    }

    if (start > end) {
        return false;
    }

    if (start == end) {
        if (start - 1 < 0 || start + 1 > getEditableText().length()) {
            return false;
        } else {
            StyleSpan[] before = getEditableText().getSpans(start - 1, start, StyleSpan.class);
            StyleSpan[] after = getEditableText().getSpans(start, start + 1, StyleSpan.class);
            return before.length > 0 && after.length > 0 && before[0].getStyle() == style && after[0].getStyle() == style;
        }
    } else {
        StringBuilder builder = new StringBuilder();

        // Make sure no duplicate characters be added
        for (int i = start; i < end; i++) {
            StyleSpan[] spans = getEditableText().getSpans(i, i + 1, StyleSpan.class);
            for (StyleSpan span : spans) {
                if (span.getStyle() == style) {
                    builder.append(getEditableText().subSequence(i, i + 1).toString());
                    break;
                }
            }
        }

        return getEditableText().subSequence(start, end).toString().equals(builder.toString());
    }
}
 
Example 15
Source File: BoldItalicSpan.java    From YCCustomText with Apache License 2.0 4 votes vote down vote up
public BoldItalicSpan() {
    super(Typeface.BOLD_ITALIC);
    type = RichTypeEnum.BOLD_ITALIC;
}
 
Example 16
Source File: AndroidPaint.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
private static Typeface getTypeface(org.oscim.backend.canvas.Paint.FontFamily fontFamily,
                                    org.oscim.backend.canvas.Paint.FontStyle fontStyle) {
    final int style;
    switch (fontStyle) {
        case BOLD:
            style = Typeface.BOLD;
            break;
        case BOLD_ITALIC:
            style = Typeface.BOLD_ITALIC;
            break;
        case ITALIC:
            style = Typeface.ITALIC;
            break;
        default:
            style = Typeface.NORMAL;
            break;
    }

    switch (fontFamily) {
        case DEFAULT:
            return Typeface.create(Typeface.DEFAULT, style);
        case DEFAULT_BOLD:
            return Typeface.create(Typeface.DEFAULT_BOLD, style);
        case MONOSPACE:
            return Typeface.create(Typeface.MONOSPACE, style);
        case SANS_SERIF:
            return Typeface.create(Typeface.SANS_SERIF, style);
        case SERIF:
            return Typeface.create(Typeface.SERIF, style);
        case THIN:
            return Typeface.create("sans-serif-thin", style);
        case LIGHT:
            return Typeface.create("sans-serif-light", style);
        case MEDIUM:
            return Typeface.create("sans-serif-medium", style);
        case BLACK:
            return Typeface.create("sans-serif-black", style);
        case CONDENSED:
            return Typeface.create("sans-serif-condensed", style);
    }

    throw new IllegalArgumentException("unknown font family: " + fontFamily);
}
 
Example 17
Source File: TypefaceCompat.java    From AppCompat-Extension-Library with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a typeface object that best matches the specified typeface and the specified style.
 * Use this call if you want to pick a new style from the same family of an typeface object.
 * If family is null, this selects from the default font's family.
 *
 * @param ctx        A context.
 * @param familyName May be null. The name of the font family.
 * @param style      The style (normal, bold, italic) of the typeface, e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC.
 * @return The best matching typeface.
 * @since 0.1.1
 * @deprecated
 */
@Deprecated
public static Typeface create(Context ctx, String familyName, int style) {
    if (!mInitialized) initialize();
    if (isSupported(familyName) || familyName == null) {
        boolean styleAfterwards = false;
        String fileName = FONT_FAMILY_FILE_PREFIX.get(familyName == null ? "sans-serif" : familyName);
        if (fileName.endsWith("-")) {
            // All styles are supported.
            fileName += STYLE_SUFFIX[style];
        } else {
            switch (style) {
                case Typeface.NORMAL:
                    break;
                case Typeface.BOLD:
                case Typeface.BOLD_ITALIC:
                    // These styles are not supported by default. Therefore force style after retrieving normal font.
                    styleAfterwards = true;
                    break;
                case Typeface.ITALIC:
                    fileName += STYLE_SUFFIX[style];
                    break;
            }
        }
        fileName += TTF_SUFFIX;
        // Retrieve Typeface from cache.
        Typeface tf = TYPEFACE_CACHE.get(fileName);
        if (tf == null) {
            // Create Typeface and cache it for later.
            String fontPath = "fonts/" + fileName;
            tf = Typeface.createFromAsset(ctx.getAssets(), fontPath);
            if (tf != null) {
                TYPEFACE_CACHE.put(fileName, tf);
            }
        }
        if (tf != null) {
            return styleAfterwards ? Typeface.create(tf, style) : tf;
        }
    }
    // Let the default implementation of Typeface try.
    return Typeface.create(familyName, style);
}
 
Example 18
Source File: Span.java    From Android-Spans with Apache License 2.0 4 votes vote down vote up
public static Node boldItalic(Object... nodes) {
    return new SpanNode(new StyleSpan(Typeface.BOLD_ITALIC), nodes);
}
 
Example 19
Source File: ARTTextShadowNode.java    From react-native-GPay with MIT License 4 votes vote down vote up
private void applyTextPropertiesToPaint(Paint paint) {
  int alignment = mTextAlignment;
  switch (alignment) {
    case TEXT_ALIGNMENT_LEFT:
      paint.setTextAlign(Paint.Align.LEFT);
      break;
    case TEXT_ALIGNMENT_RIGHT:
      paint.setTextAlign(Paint.Align.RIGHT);
      break;
    case TEXT_ALIGNMENT_CENTER:
      paint.setTextAlign(Paint.Align.CENTER);
      break;
  }
  if (mFrame != null) {
    if (mFrame.hasKey(PROP_FONT)) {
      ReadableMap font = mFrame.getMap(PROP_FONT);
      if (font != null) {
        float fontSize = DEFAULT_FONT_SIZE;
        if (font.hasKey(PROP_FONT_SIZE)) {
          fontSize = (float) font.getDouble(PROP_FONT_SIZE);
        }
        paint.setTextSize(fontSize * mScale);
        boolean isBold =
            font.hasKey(PROP_FONT_WEIGHT) && "bold".equals(font.getString(PROP_FONT_WEIGHT));
        boolean isItalic =
            font.hasKey(PROP_FONT_STYLE) && "italic".equals(font.getString(PROP_FONT_STYLE));
        int fontStyle;
        if (isBold && isItalic) {
          fontStyle = Typeface.BOLD_ITALIC;
        } else if (isBold) {
          fontStyle = Typeface.BOLD;
        } else if (isItalic) {
          fontStyle = Typeface.ITALIC;
        } else {
          fontStyle = Typeface.NORMAL;
        }
        // NB: if the font family is null / unsupported, the default one will be used
        paint.setTypeface(Typeface.create(font.getString(PROP_FONT_FAMILY), fontStyle));
      }
    }
  }
}
 
Example 20
Source File: UDSpannableString.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
private void initSpannableStringBuilder(LuaValue text, LuaValue config) {
    SpannableStringBuilder spannableStringBuilder = getSpannableStringBuilder();
    if (text != null && text.isstring()) {
        spannableStringBuilder = spannableStringBuilder.append(text.tojstring());
    }

    if (spannableStringBuilder.length() > 0) {
        if (config != null && config.istable()) {
             int end = spannableStringBuilder.length();
             int fontSize = DimenUtil.spToPx(config.get("fontSize").optint(-1));
             Integer fontColor = ColorUtil.parse(LuaUtil.getValue(config, "fontColor"));
             String fontName = config.get("fontName").optjstring(null);

             LuaValue weightValue = config.get("fontWeight");
             int fontWeight = LuaUtil.isNumber(weightValue) ? weightValue.optint(UDFontWeight.WEIGHT_NORMAL_INT) : UDFontWeight.getValue(weightValue.optjstring(UDFontWeight.WEIGHT_NORMAL));

             LuaValue styleValue = config.get("fontStyle");
             int fontStyle = LuaUtil.isNumber(styleValue) ? styleValue.optint(Typeface.NORMAL) : UDFontStyle.getValue(styleValue.optjstring(UDFontStyle.STYLE_NORMAL));

             Integer backgroundColor = ColorUtil.parse(LuaUtil.getValue(config, "backgroundColor"));
             boolean strikethrough = config.get("strikethrough").optboolean(false);
             boolean underline = config.get("underline").optboolean(false);

            if (fontSize != -1) {//字体大小
                spannableStringBuilder.setSpan(new AbsoluteSizeSpan(fontSize), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontColor != null) {//字体颜色
                spannableStringBuilder.setSpan(new ForegroundColorSpan(fontColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontName != null && getLuaResourceFinder() != null) {//字体
                spannableStringBuilder.setSpan(new CustomTypefaceSpan(fontName, getLuaResourceFinder().findTypeface(fontName)), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontWeight != -1 && fontWeight > UDFontWeight.WEIGHT_NORMAL_INT) {//文字Weight
                spannableStringBuilder.setSpan(new WeightStyleSpan(fontWeight), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (fontStyle != -1 && (fontStyle >= Typeface.NORMAL && fontStyle <= Typeface.BOLD_ITALIC)) {//文字样式
                spannableStringBuilder.setSpan(new StyleSpan(fontStyle), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (backgroundColor != null) {//背景色
                spannableStringBuilder.setSpan(new BackgroundColorSpan(backgroundColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (strikethrough) {//删除线
                spannableStringBuilder.setSpan(new StrikethroughSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (underline) {//下划线
                spannableStringBuilder.setSpan(new UnderlineSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}