Any way to setTypeFace from url for a Textview?

淺唱寂寞╮ 提交于 2020-02-02 16:21:24

问题


I want to setTypeFace from url like : https://github.com/anak10thn/frontend-homepage/raw/master/font/helvetica-regular.otf

Do you know if there any way ?

I tried this :

                Typeface tf = Typeface.createFromFile("https://github.com/anak10thn/frontend-homepage/raw/master/font/helvetica-regular.otf");

            tv.setTypeface(tf);

It doesn't work.

I have this error :

03-24 04:46:59.931    1314-1314/bya.fchps.buildyourapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bya.fchps.buildyourapp, PID: 1314
java.lang.RuntimeException: Unable to start activity ComponentInfo{bya.fchps.buildyourapp/com.fchps.buildyourapp.views.ClassicView}: java.lang.RuntimeException: native typeface cannot be made
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: native typeface cannot be made
        at android.graphics.Typeface.<init>(Typeface.java:175)
        at android.graphics.Typeface.createFromFile(Typeface.java:159)
        at com.fchps.buildyourapp.views.ClassicView.setClassicLabel(ClassicView.java:178)
        at         com.fchps.buildyourapp.views.ClassicView.onCreate(ClassicView.java:75)
        at android.app.Activity.performCreate(Activity.java:5231)
        at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at            android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

回答1:


Hi You can not directly add the URL of font in Typeface. If you want to dynamically use the font in your application, the best option is application would download fist font in local device. Then define the path of font in to the Typeface.

Typeface typeface = Typeface.createFromFile(
    new File(Environment.getExternalStorageDirectory(), "font.ttf"));



回答2:


I dont believe you can actually. The custom typeface is usually kept as a system resource file, and not referred to as a web document. As far as i know, the best method would most likely be: Create a fonts folder in your assets folder. Download the font you desire and place it in the fonts folder, and then in the code refer back to your fonts folder. For further detailed instructions refer to this website: http://www.tutorialspoint.com/android/android_custom_fonts.htm



来源:https://stackoverflow.com/questions/29225276/any-way-to-settypeface-from-url-for-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!