iOS--UILabel字体默认宽度和高度

来源:互联网 发布:linux系统入门教程 编辑:程序博客网 时间:2024/06/05 06:35

iOS--UILabel字体默认宽度和高度 

标签: ios字体uilabel宽高
 729人阅读 评论(0) 收藏 举报
 分类:
 

目录(?)[+]

今天发现了UILabel的一个属性:intrinsicContentSize,这个属性能返回UILabel对象的内容宽高(1行文本)。

宽度

接着发现UILabel对象的文字宽度是由文字的font属性和文字个数确定的,而且中文、英文、数字的宽度计算还不一样,我认为的一个原因是iOS默认的字体中,中文是等宽字体,而英文和数字并不是等宽字体。

  • 中文 
    由于等宽,所以很好计算,包括全月角标点符号也是可以这么计算的,就是:每个中文文字的宽度 * 文字个数。文字个数就很好确定了,但每个中文文字的宽度是怎样确定的呢。通过一个小测试可知,中文文字的宽度是由UILabel的font属性确定的,font属性设置了多大号的字体,中文问题宽度就为多大号,即两者呈线性关系(PS:这个关系的前提是使用系统默认的字体。此外,若没有显式设置font属性的话,UILabel默认的字体大小为17),如:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.font</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>];      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 每个中文字宽度就为12</span>label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.font</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>];      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 系统默认字体大小,每个中文文字宽度为17</span>label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.font</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>];      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 每个中文文字宽度为20</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
  • 数字 
    数字在系统默认字体内就不是等宽的了,所以宽度也会随着数字的不同而不同,下面是字体大小为系统默认字体大小(即17)的数字宽度表:
数字宽度
0
1013
1
713
2
10
3
1013
4
1023
5
1013
6
1023
7
913
8
1023
9
1023
  • 英文 
    英文在系统默认字体中也是不等宽的,所以宽度也是会随着字母的不同而不同,下面是字体大小为系统默认字体大小(即17)的数字宽度表:
小写字母宽度大写字母宽度a
9
A
1113
b
1013
B
11
c
913
C
12
d
1013
D
12
e
913
E
10
f
6
F
923
g
10
G
1213
h
10
H
1213
i
4
I
413
j
4
J
9
k
9
K
11
l
4
L
913
m
1423
M
1423
n
923
N
1213
o
923
O
13
p
10
P
1023
q
10
Q
13
r
613
R
11
s
823
S
1023
t
6
T
1023
u
923
U
1213
v
9
V
1113
w
13
W
1613
x
823
X
1113
y
9
Y
11
z
9
Z
11

高度

UILabel的高度就貌似没有线性关系了:

字体大小高度10
12
11
1313
12
1413
13
1523
14
17
15
18
16
1913
17
2013
18
2123
19
23
20
24
21
2513
22
2613
23
2723
24
2823
25
30
26
3113
27
3213
28
3323
29
3423
0 0