如何在 UILabel 中使字符等宽 [英] how to make characters equal width in UILabel
问题描述
我在使用 UILabel
时遇到问题.
I have a problem when using UILabel
.
我这里有两个标签(上图),字体相等,宽度相等,textAlignment都是左对齐,都是10个字符,但是每个字符的宽度不同,所以不能一一对齐,我'我正在尝试动态添加间距,但我没有这样做,那么我该如何解决呢?非常感谢~
I have two labels here(above images), they have equal font and equal width, textAlignment are both left, they both have 10 characters, but each character have different width so it can't be aligned one by one, I‘m trying to add spacing dynamically but I failed to do that, so how can I fix it? thanks a lot~
推荐答案
失败的原因是您使用了比例字体,意思是:字符将占据单独的宽度.一个 i
只会承受一个 m
的摩擦.
The reason it fails is you are using a proportional font, meaning: characters will take up an individual width. An i
will just take a friction of a m
.
使用等宽字体,因为所有字符都将具有相同的宽度.
Use a monospaced font, than all characters will have the same width.
label.font = UIFont(name:"Courier", size: 17)
出于同样的原因,在代码编辑器中使用 Courier 和其他等宽字体.
for the same reason Courier and other monospaced fonts are used in code editors.
这篇关于如何在 UILabel 中使字符等宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!