疑难杂症

安卓系统自带的Textview可以设置粗体、斜体、正常字体,但是设计经常设计想比普通字体粗一点(PingFangSC-Medium)又不想Bold效果那么粗,所谓的“中粗”。

IOS是支持的,之前安卓这边经常回设计的是: 或者 不粗,做一个选择。

寻找思路

难道安卓真的不能达到“中粗”的效果吗?其实是可以的。通过读TextView的源码可以看到,在onDraw()中,最终使用mTextPaint 完成文字的绘制,而使用 mTextPaint 又实现文字颜色和是否粗细及文字大小等属性的控制。

mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);mTextPaint.setTextSize(size);mTextPaint.setColor(color);

而TextPaint的父类是谁呢?是Paint,找到Paint后,就会发现Paint有一个方法是 setStrokeWidth,可以设置画笔的粗细。那么通过设置Paint的StrokeWidth,是否可以达到控制文字粗细的效果呢,其实是可以的。

只要在onDraw()之前调用getPaint()拿到mTextPaint,通过setStrokeWidth设置画笔的粗细,就可以达到控制TextView的文字粗细效果。

@Overrideprotected void onDraw(Canvas canvas) {//获取当前控件的画笔TextPaint paint = getPaint();//设置画笔的描边宽度值paint.setStrokeWidth(mStrokeWidth);paint.setStyle(Paint.Style.FILL_AND_STROKE);super.onDraw(canvas);}

完整代码及Demo

为了更好的使用,自定义BoldTextView 继承TextView,添加自定义属性stroke_width控制文字粗细,默认0.5比正常字体稍粗(0为正常不加粗);0以上可实现逐渐加粗;0.5~1大概可以看到中粗的效果;2大约是Bold的效果。

代码

<!-- 加粗字体 --><declare-styleable name="BoldTextView"><attr name="stroke_width" format="float"/></declare-styleable>
/*** 加粗字体* @author by Zyq* @date 2022/3/15.*/
public class BoldTextView extends AppCompatTextView {/*** 数值越大,字体越粗,0.0f表示常规画笔的宽度,相当于默认情况*/private float mStrokeWidth = 0.5f;public BoldTextView(Context context) {this(context,null);}public BoldTextView(Context context, AttributeSet attrs) {this(context, attrs,0);}public BoldTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//获取xml定义属性TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BoldTextView,defStyleAttr,0);mStrokeWidth = array.getFloat(R.styleable.BoldTextView_stroke_width,mStrokeWidth);}@Overrideprotected void onDraw(Canvas canvas) {//获取当前控件的画笔TextPaint paint = getPaint();//设置画笔的描边宽度值paint.setStrokeWidth(mStrokeWidth);paint.setStyle(Paint.Style.FILL_AND_STROKE);super.onDraw(canvas);}public void setStrokeWidth(float mStrokeWidth) {this.mStrokeWidth = mStrokeWidth;invalidate();}public float getStrokeWidth() {return mStrokeWidth;}
}

使用

使用很简单

<cn.android.view.text.BoldTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginTop="20dp"android:text="看看加粗效果"android:textColor="#333333"android:textSize="20sp" />
<cn.android.view.text.BoldTextViewandroid:text="看看加粗效果"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_alignParentRight="true"app:stroke_width="1"android:textColor="#333333"android:textSize="20sp"/>

多个实现对比加粗效果

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".text.BoldTextActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:text="常规文本"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#999999"android:textSize="10sp"/><TextViewandroid:text="看看加粗效果"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_alignParentRight="true"android:textColor="#333333"android:textSize="20sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:text="普通加粗文本:bold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#999999"android:textSize="10sp"/><TextViewandroid:text="看看加粗效果"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:textStyle="bold"android:layout_alignParentRight="true"android:textColor="#333333"android:textSize="20sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自定义BoldTextView:默认strokeWidth=0.5中粗"android:textColor="#999999"android:textSize="10sp" /><cn.android.view.text.BoldTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginTop="20dp"android:text="看看加粗效果"android:textColor="#333333"android:textSize="20sp" /><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:text="自定义BoldTextView:strokeWidth=0.0不加粗"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#999999"android:textSize="10sp"/><cn.android.view.text.BoldTextViewandroid:text="看看加粗效果"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_alignParentRight="true"app:stroke_width="0"android:textColor="#333333"android:textSize="20sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自定义BoldTextView:strokeWidth=2"android:textColor="#999999"android:textSize="10sp" /><cn.android.view.text.BoldTextViewandroid:text="看看加粗效果"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_alignParentRight="true"app:stroke_width="2"android:textColor="#333333"android:textSize="20sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自定义BoldTextView:点击右侧文本动态改变strokeWidth+0.5"android:textColor="#999999"android:textSize="10sp" /><TextViewandroid:id="@+id/tvShowStoke"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="strokeWidth=0"android:layout_marginTop="30dp"android:textColor="#999999"android:textSize="10sp" /><cn.android.view.text.BoldTextViewandroid:id="@+id/tvDynamicStoke"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginTop="20dp"android:text="看看加粗效果"android:textColor="#333333"android:textSize="20sp"app:stroke_width="0" /><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:background="#e2e2e2"/></RelativeLayout></LinearLayout>
/*** 加粗文本*/
public class BoldTextActivity extends Activity {BoldTextView tvDynamicStoke;TextView tvShowStoke;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bold_text);tvShowStoke= (TextView) findViewById(R.id.tvShowStoke);tvDynamicStoke= (BoldTextView)findViewById(R.id.tvDynamicStoke);tvDynamicStoke.setOnClickListener((v)->{float strokeWidth = tvDynamicStoke.getStrokeWidth()+0.5f;tvShowStoke.setText("strokeWidth="+strokeWidth);tvDynamicStoke.setStrokeWidth(strokeWidth);});}
}

效果

使用TextPaint实现自由加粗字体:BoldTextView,支持中粗相关推荐

  1. android粗字体下载,Android中粗字体

    前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页面里面有普通字体.中粗字体.加粗字体.对于IOS的小伙伴,分分钟搞定,但是对于Android开发的我,瞬间懵逼了.WTF! 安卓只有粗和不 ...

  2. 前端怎么加粗字体_to B 中后台系统 | Web 端 | UI Style Guideline amp; 前端交付文档...

    本文总结近期 to B 中后台系统 UI 组件设计规范及相关经验.欢迎大家勘别甄误. 不懂 UI 的交互不是好前端,特别鸣谢 松若章 在设计稿落地过程中的严谨与指点. 组件规范 Components ...

  3. android中弹出消息字体设置,Android如何设置中粗字体——自定义textview

    最近UI出了一版新的UI效果图,里面的标题用的都是中粗字体.这个中粗字体对于ios来说是很容易的,因为ios本省就自带中粗字体,但是对于安卓来说就没那么简单了,因为安卓中的textview只有标准字体 ...

  4. Android如何设置中粗字体——自定义textview

    最近UI出了一版新的UI效果图,里面的标题用的都是中粗字体.这个中粗字体对于ios来说是很容易的,因为ios本省就自带中粗字体,但是对于安卓来说就没那么简单了,因为安卓中的textview只有标准字体 ...

  5. ios 自动缩小字体_小字体紫筑B丸85%中粗体文件+deb双版本

    今天带来的是有字由心雨公众号的一款收费字体『紫筑B丸』,我把5字重里面的中粗体提取出来单独缩小至85%后做成了单字重,同时把英文也改变成和中文对应的大小粗度后,整体感觉非常的舒服,也一直是我自用最久的 ...

  6. html字体怎么变大变粗,ppt如何加粗字体更粗 幻灯片的艺术字怎么把字体变粗?...

    怎么样能使PPT中所有的字体一次性加粗,用CTRA+A全选只能选中幻灯片而不在电脑桌面上找到PPT演示文稿.并将PPT演示文稿双击打开. 打开了文稿之后,在幻灯片里面找到大纲视图,并点击它,将幻灯片切 ...

  7. iOS 加粗字体方法 (不改变字体字号只加粗文字)

    /*** 加粗字体方法 * label 要加粗的 UILabel* BOOL YES = 加粗字体 NO = 去掉字体加粗*/- (void)jiaCuFontFormLabel:(UILabel * ...

  8. 【PIL处理图片】小技巧之画虚线、加粗字体、长文本自动分行(符号处理)

    [PIL处理图片]系列文章目录 小技巧之图片透明渐变处理 小技巧之画虚线.加粗字体.长文本自动分行(符号处理) 小技巧之圆角边框处理 上一篇介绍了图片渐变蒙版处理,还有一些其他的小技巧,一起在这里介绍 ...

  9. Markdown (CSDN) MD编辑器(二)- 文本样式(更改字体、字体大小、字体颜色、加粗、斜体、高亮、删除线)

    目录 1.Markdown现有的文本样式. 2.HTML的font标签-改字体.字体颜色.字体大小. 3.HTML的mark标签-标记文本 4.HTML的strong标签-加粗文本 5.HTML的em ...

最新文章

  1. 将功能绑定到Twitter Bootstrap Modal关闭
  2. 免费科研数据集大搜索,来源于互联网~
  3. Nginx+PHP7 安装及配置
  4. matlab中怎么使用disp函数显示一句话同时输出变量值呢?
  5. 来了!小米9发布时间确定:2月20日见 为你而战
  6. web下拉列表代码_文章列表总结(一)
  7. AngularJS Provider/Service/Factory 使用
  8. 五面拿下阿里飞猪offer,java基础知识梳理
  9. Xcode打包后,找不到dSYM文件
  10. 大数据开发工程师招聘要求高吗?
  11. Android解决滑动冲突
  12. 破晓博客-自定义标签的开发
  13. 越来越大的人使用计算机的原因,为什么越来越多的人喜欢用WPS这款电脑软件?这几点是关键原因...
  14. win10家庭版(阉割版)打开远程桌面服务
  15. 大规模并行 量子计算机,QC资讯丨IBM发布量子路线图——将在2023年底推出1000量子比特量子计算机...
  16. vivo计算机的隐藏功能介绍,vivo手机13个隐藏功能介绍,你知道几个?
  17. python通过路径找文件_Python寻找路径和查找文件路径的示例
  18. ArcGIS导出地图是全黑的
  19. 2016年linux认证考试,2016年Linux认证考试模拟练习及答案
  20. 图像处理中的边缘检测

热门文章

  1. 2022全国水下机器人大赛国际线上赛来啦!“水下感知赛、通信赛”等你来战!
  2. 【全文翻译】YOLOv4:目标检测的最佳速度和准确性
  3. react中的 .d.ts
  4. 图片加载异常兜底方案
  5. 安装mysql数据库
  6. Arduino Leonardo教程:如何回车,特殊按键定义,DIY超便宜的键盘主控
  7. 发送短信(SMS)承载方式有哪些?
  8. QuartusⅡ开发alteraFPGA如何约束时钟(关于消除警告Timing requirements not met)
  9. dbus-1 not met问题
  10. vscode 过程试图写入的管道不存在