分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
今天发现了UILabel的一个属性:intrinsicContentSize,这个属性能返回UILabel对象的内容宽高(1行文本)。
宽度
接着发现UILabel对象的文字宽度是由文字的font属性和文字个数确定的,而且中文、英文、数字的宽度计算还不一样,我认为的一个原因是iOS默认的字体中,中文是等宽字体,而英文和数字并不是等宽字体。
- 中文
由于等宽,所以很好计算,包括全月角标点符号也是可以这么计算的,就是:每个中文文字的宽度 * 文字个数。文字个数就很好确定了,但每个中文文字的宽度是怎样确定的呢。通过一个小测试可知,中文文字的宽度是由UILabel的font属性确定的,font属性设置了多大号的字体,中文问题宽度就为多大号,即两者呈线性关系(PS:这个关系的前提是使用系统默认的字体。此外,若没有显式设置font属性的话,UILabel默认的字体大小为17),如:
- 数字
数字在系统默认字体内就不是等宽的了,所以宽度也会随着数字的不同而不同,下面是字体大小为系统默认字体大小(即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
|