澳门皇冠金沙网站-澳门皇冠844网站

热门关键词: 澳门皇冠金沙网站,澳门皇冠844网站

Android修改字体样式,Runtime使用之全局字体替换为

最近在使用custom fontFamily的时候遇到一个小坑.

}别忘了#import解析一下上面这几句代码

皇冠手机登陆博彩 1

皇冠送现金,在RCTConvert.m中,

如果要交换的是实例方法,那么就要用class_getInstanceMethod 获取

  • 皇冠手机登陆博彩,casual
  • cursive
  • serif
  • monospace
  • sans-serif
  • sans-serif-condensed
  • serif-monospace
  • sans-serif-smallcaps

我发现我在使用名为'Impat'的自定义字体时,

遍历代码如下

※typeface和fontFamily区别

android:typeface属性是增加API1android:fontFamily在API16中添加了属性

我不明白为什么React Native的作者们, 要在这里判断一定是isItalic isCondensed 才有对应的逻辑, 如果不是, 就返回nil了.

然后在Build Phases里添加资源文件 如图

皇冠hg3088,通过设置typeface属性或者fontFamily属性设置typeface属性:

return false 所以 bestMatch return nil.最终导致我无法正确取到这个font.

     ------------------------------华丽的分割线--------------------------

※当同时设置typeface和fontFamily时,只有fontFamily生效

查看一波TextView的源码

 private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) { Typeface tf = null; if (familyName != null) { tf = Typeface.create(familyName, styleIndex); if (tf != null) { setTypeface; return; } } switch (typefaceIndex) { case SANS: tf = Typeface.SANS_SERIF; break; case SERIF: tf = Typeface.SERIF; break; case MONOSPACE: tf = Typeface.MONOSPACE; break; } setTypeface(tf, styleIndex); }

从方法setTypefaceFromAttrs()看,如果你有set fontFamily属性,那么typefaceattribute将被忽略。

这边会发现这样设置typeface和fontFamily属性对中文不生效,这时候就需要引用外部的字体样式(这里谷歌设计规范推荐使用NOTO字体

在assets下新建一个fonts文件,把字体样式文件放进去

皇冠手机登陆博彩 2

在代码中设置

AssetManager mgr = getAssets();Typeface tf = Typeface.createFromAsset(mgr, "fonts/NotoSansCJKsc-Black.otf");tv_1.setTypeface;

public class CustomTextView extends TextView{ public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } //重写设置字体方法 @Override public void setTypeface(Typeface tf) { tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/NotoSansCJKsc-Light.otf"); super.setTypeface; }}

之后在XML布局文件中使用CustomTextView代替TextView

 <com.test.fontfamily.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" android:text="自定义字体" android:textSize="24dp" />

皇冠手机登陆博彩 3

思路:遍历找到所有的TextView然后替换字体百度了一下找到下面工具类

package com.test.fontfamily;import android.app.Application;import android.content.Context;import android.graphics.Typeface;import android.support.annotation.NonNull;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import java.lang.ref.SoftReference;import java.lang.reflect.Field;import java.util.HashMap;import java.util.Map;/** * Created by Administrator on 2017/10/24. */public class FontUtils{ private static final String TAG = FontUtils.class.getSimpleName(); private Map<String, SoftReference<Typeface>> mCache = new HashMap<>(); private static FontUtils sSingleton = null; public static Typeface DEFAULT = Typeface.DEFAULT; // disable instantiate private FontUtils() { } public static FontUtils getInstance() { // double check if (sSingleton == null) { synchronized (FontUtils.class) { if (sSingleton == null) { sSingleton = new FontUtils(); } } } return sSingleton; } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath font file path relative to 'assets' directory. */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath)); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath font file path relative to 'assets' directory. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath The full path to the font data. */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromFile); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath The full path to the font data. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromFile, style); } /** * <p>Replace the font of specified view and it's children with specified typeface</p> */ private void replaceFont(@NonNull View root, @NonNull Typeface typeface) { if (root == null || typeface == null) { return; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView =  root; // Extract previous style of TextView int style = Typeface.NORMAL; if (textView.getTypeface() != null) { style = textView.getTypeface().getStyle(); } textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount { replaceFont(viewGroup.getChildAt, typeface); } } // else return } /** * <p>Replace the font of specified view and it's children with specified typeface and text style</p> * * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style) { if (root == null || typeface == null) { return; } if (style < 0 || style > 3) { style = Typeface.NORMAL; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView =  root; textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount { replaceFont(viewGroup.getChildAt, typeface, style); } } // else return } /** * <p>Create a Typeface instance with specified font file</p> * * @param fontPath font file path relative to 'assets' directory. * @return Return created typeface instance. */ private Typeface createTypefaceFromAsset(Context context, String fontPath) { SoftReference<Typeface> typefaceRef = mCache.get; Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get == null) { typeface = Typeface.createFromAsset(context.getAssets(), fontPath); typefaceRef = new SoftReference<>; mCache.put(fontPath, typefaceRef); } return typeface; } private Typeface createTypefaceFromFile(String fontPath) { SoftReference<Typeface> typefaceRef = mCache.get; Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get == null) { typeface = Typeface.createFromFile; typefaceRef = new SoftReference<>; mCache.put(fontPath, typefaceRef); } return typeface; } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> * * @param context {@link Context Context} * @param fontPath font file path relative to 'assets' directory. */ public void replaceSystemDefaultFontFromAsset(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromAsset(context, fontPath)); } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> * * @param context {@link Context Context} * @param fontPath The full path to the font data. */ public void replaceSystemDefaultFontFromFile(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromFile); } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> */ private void replaceSystemDefaultFont(@NonNull Typeface typeface) { modifyObjectField(null, "MONOSPACE", typeface); } private void modifyObjectField(Object obj, String fieldName, Object value) { try { Field defaultField = Typeface.class.getDeclaredField(fieldName); defaultField.setAccessible; defaultField.set(obj, value); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }}

阅读源码发现这里面主要方法有

  • ###### replaceFont()

替换一个页面中的所有字体用递归的方式去查找view是否是TextView或者TextView的子类,然后进行替换不过这种方法效率不高用法:在页面中FontUtils.getInstance().replaceFontFromAsset(View root, String fontPath)

  • ###### modifyObjectField()

替换App所有字体利用反射替换系统默认字体用法:a.新建一个BaseApplication继承Application在onCreate方法中FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this,"fonts/NotoSansCJKsc-Thin.otf");b.在AppTheme中添加<item name="android:typeface">monospace</item>c.清单文件中使用BaseApplication

在一个类似的issue下提了: , 等有时间仔细看看这里.

首先获取我们的第三方字体,若字体不存在,则返回系统默认字体

fontFamily属性:

暂时的处理是:

具体的实现

  • normal
  • serif
  • sans
  • monospace

正常的使用方法可以参照这里: fontFamily的时候,发现无论如何都不能正确取到这个font, 而其他的custom fontFamily则是可以的. 于是进去看源码.

 dispatch_once(&onceToken, ^{ Method oldMethod = class_getClassMethod([self class], @selector(systemFontOfSize:)); Method newMethod = class_getClassMethod([self class], @selector(__nickyfontchanger_YaheiFontOfSize:)); method_exchangeImplementations(oldMethod, newMethod); }); 

在Android实际开发中根据UI的设计图,经常要去改变系统默认的字体样式这样做会使apk变大很多啊而且为什么android要使用ios的字体-_-#

最下边有段这样的代码:

 static dispatch_once_t onceToken;

  UIFont:font withFamily:family size:size weight:weight style:style scaleMultiplier:scaleMultiplier

因为方法已经交换了,实际上这个方法的pointer指向的是系统的systemFontOfSize这个方法

isItalic == RCTFontIsItalic && isCondensed == RCTFontIsCondensed

皇冠手机登陆博彩 4

 UIFont *bestMatch = font; CGFloat closestWeight = INFINITY; for (NSString *name in [UIFont fontNamesForFamilyName:familyName]) { UIFont *match = [UIFont fontWithName:name size:fontSize]; if (isItalic == RCTFontIsItalic && isCondensed == RCTFontIsCondensed { CGFloat testWeight = RCTWeightOfFont; if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) { bestMatch = match; closestWeight = testWeight; } } }return bestMatch;

先获取旧的方法,再获取新的方法,新的方法是写在这个category里的

 // Get the closest font that matches the given weight for the fontFamily UIFont *bestMatch = font; CGFloat closestWeight = INFINITY; for (NSString *name in [UIFont fontNamesForFamilyName:familyName]) { UIFont *match = [UIFont fontWithName:name size:fontSize]; if (isItalic == RCTFontIsItalic && isCondensed == RCTFontIsCondensed { CGFloat testWeight = RCTWeightOfFont; if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) { bestMatch = match; closestWeight = testWeight; } else { // else是我加的 bestMatch = match; } } else { // else是我加的. 使用"Impact"字体时,并没有走这个对应的if.导致bestMatch为nil bestMatch = match; } } return bestMatch;

导致整个工程的字体配置可能存在修改工作量大,改漏改错等情况,针对这种情况我们也可以通过runtime来解决。

皇冠手机登陆博彩 5

1、导入第三方字体

本文由澳门皇冠金沙网站发布于编辑程序,转载请注明出处:Android修改字体样式,Runtime使用之全局字体替换为