@ReactProp(name = "collapsedTitleTypeface") public void setCollapsedTitleTypeface(CollapsingToolbarLayoutView view, String font) { try { Typeface face = ReactFontManager.getInstance().getTypeface(font, Typeface.NORMAL, view.getContext().getAssets()); view.setCollapsedTitleTypeface(face); } catch (Exception e) { } }
@ReactProp(name = "expandedTitleTypeface") public void setExpandedTitleTypeface(CollapsingToolbarLayoutView view, String font) { try { Typeface face = ReactFontManager.getInstance().getTypeface(font, Typeface.NORMAL, view.getContext().getAssets()); view.setExpandedTitleTypeface(face); } catch (Exception e) { } }
@ReactProp(name = ViewProps.FONT_FAMILY) public void setFontFamily(ReactEditText view, String fontFamily) { int style = Typeface.NORMAL; if (view.getTypeface() != null) { style = view.getTypeface().getStyle(); } Typeface newTypeface = ReactFontManager.getInstance().getTypeface( fontFamily, style, view.getContext().getAssets()); view.setTypeface(newTypeface); }
/** * fontStyle: NORMAL = 0, BOLD = 1, ITALIC = 2, BOLD_ITALIC = 3 */ public static Typeface parseTypeface(Context context, ReadableMap propMap, String styleKey, String familyKey) { String fontFamily = null; if (propMap.hasKey(familyKey)) { fontFamily = propMap.getString(familyKey); } int style = 0; if (propMap.hasKey(styleKey)) { style = propMap.getInt(styleKey); } return ReactFontManager.getInstance().getTypeface(fontFamily, style, context.getAssets()); }
@ReactMethod public void loadFont(final ReadableMap options, final Callback callback) throws Exception { Activity currentActivity = getCurrentActivity(); if (currentActivity == null) { callback.invoke("Invalid activity"); return; } String name = (options.hasKey("name")) ? options.getString("name") : null, data = (options.hasKey("data")) ? options.getString("data") : null, type = null; if (name == null || name.length() == 0) { callback.invoke("Name property empty"); return; } if (data == null || data.length() == 0) { callback.invoke("Data property empty"); return; } if (("data:").equalsIgnoreCase(data.substring(0, 5))) { Integer pos = data.indexOf(','); if (pos > 0) { String[] encodingParams = data.substring(5, pos).split(";"); String mimeType = encodingParams[0]; data = data.substring(pos + 1); if (("application/x-font-ttf").equalsIgnoreCase(mimeType) || ("application/x-font-truetype").equalsIgnoreCase(mimeType) || ("font/ttf").equalsIgnoreCase(mimeType)) { type = "ttf"; } else if (("application/x-font-opentype").equalsIgnoreCase(mimeType) || ("font/opentype").equalsIgnoreCase(mimeType)) { type = "otf"; } } } if (options.hasKey("type")) type = options.getString("type"); if (type == null) type = "ttf"; try { byte[] decodedBytes = Base64.decode(data, Base64.DEFAULT); File cacheFile = new File(currentActivity.getCacheDir(), "tempFont" + (tempNameCounter++) + type); FileOutputStream stream = new FileOutputStream(cacheFile); try { stream.write(decodedBytes); } finally { stream.close(); } //Load the font from the temporary file we just created Typeface typeface = Typeface.createFromFile(cacheFile); //Cache the font for react ReactFontManager.getInstance().setTypeface(name, typeface.getStyle(), typeface); cacheFile.delete(); } catch(Exception e) { callback.invoke(e.getMessage()); } finally { callback.invoke(null, name); } }