作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在让我的自定义字体显示为粗体时遇到了一些麻烦,这是我正在尝试做的事情:
我在 res/string 中设置了一个 HTML 格式的字符串
<string name="string1">
<![CDATA[<b>Title1</b><p>sub text</p>]]>
</string>
然后我在运行时设置字体(其中 getString 从字符串中获取 resID):
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf");
TextView body = (TextView) findViewById(R.id.tv_body);
body.setText(Html.fromHtml(getString(name)));
body.setTypeface(tf);
问题是文本在应该显示的地方没有显示为粗体,我哪里出错了??
抱歉忘了补充一点,我需要一些粗体文本,一些不需要。
最佳答案
使用:
body.setTypeface(tf, Typeface.BOLD);
您还可以在 webView 上设置字体。只需添加如下内容:
<html> <head><style type=\"text/css\"> @font-face { font-family: MyFont; src: url(\"file:///android_asset/font.ttf\") } body { font-family: MyFont; font-size: 12px; text-align: justify; } </style> </head><body>
并加载使用:
webView.loadDataWithBaseURL("file:///android_asset/", result, "text/html", "UTF-8", "null");
//对于已编辑的问题
String s = getResources().getString(R.string.string1);
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);
关于Android 使用自定义字体将 HTML 文本显示为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332964/
下面是我用来制作 1px 文本描边轮廓的代码。但是如何使轮廓变粗呢?如果我只是用“5px”替换所有“1px”,结果看起来很疯狂。 HTML Hello! CSS .element { color:
我是一名优秀的程序员,十分优秀!