前言
本文主要給大家介紹了關(guān)于ios label全方位對齊的實現(xiàn)方法,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧
aruilabeltextalign
1. 實現(xiàn) uilabel文本在 左(上 中 下)、中(上 中 下)、右(上 中 下) 9個方位顯示;
2. 提供富文本底部不對齊的解決方案;
演示
核心代碼:
aralignlabel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#import <uikit/uikit.h> @ class armaker; typedef ns_enum(nsuinteger, textaligntype) { textaligntype_top = 10, // 頂部對齊 textaligntype_left, // 左邊對齊 textaligntype_bottom, // 底部對齊 textaligntype_right, // 右邊對齊 textaligntype_center // 水平/垂直對齊(默認中心對齊) }; @interface aralignlabel : uilabel /** * 根據(jù)對齊方式進行文本對齊 * * @param aligntype 對齊block */ - ( void )textalign:( void (^)(armaker *make))aligntype; @end //工具類 @interface armaker : nsobject /* 存放對齊樣式 */ @property(nonatomic, strong) nsmutablearray *typearray; /** * 添加對齊樣式 */ - (armaker *(^)(textaligntype type))addaligntype; @end |
aralignlabel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#import "aralignlabel.h" @interface aralignlabel () /* 對齊方式 */ @property(nonatomic, strong) nsarray *typearray; //上 @property(nonatomic, assign) bool hastop; //左 @property(nonatomic, assign) bool hasleft; //下 @property(nonatomic, assign) bool hasbottom; //右 @property(nonatomic, assign) bool hasright; @end @implementation aralignlabel - (cgrect)textrectforbounds:(cgrect)bounds limitedtonumberoflines:(nsinteger)numberoflines { cgrect textrect = [super textrectforbounds:bounds limitedtonumberoflines:numberoflines]; if (self.typearray){ for ( int i=0; i<self.typearray.count; i++) { textaligntype type = [self.typearray[i] integervalue]; switch (type) { case textaligntype_top: //頂部對齊 self.hastop = yes; textrect.origin.y = bounds.origin.y; break ; case textaligntype_left: //左部對齊 self.hasleft = yes; textrect.origin.x = bounds.origin.x; break ; case textaligntype_bottom: //底部對齊 self.hasbottom = yes; textrect.origin.y = bounds.size.height - textrect.size.height; break ; case textaligntype_right: //右部對齊 self.hasright = yes; textrect.origin.x = bounds.size.width - textrect.size.width; break ; case textaligntype_center: if (self.hastop) { //上中 textrect.origin.x = (bounds.size.width - textrect.size.width)*0.5; } else if (self.hasleft) { //左中 textrect.origin.y = (bounds.size.height - textrect.size.height)*0.5; } else if (self.hasbottom) { //下中 textrect.origin.x = (bounds.size.width - textrect.size.width)*0.5; } else if (self.hasright) { //右中 textrect.origin.y = (bounds.size.height - textrect.size.height)*0.5; } else { //上下左右居中 textrect.origin.x = (bounds.size.width - textrect.size.width)*0.5; textrect.origin.y = (bounds.size.height - textrect.size.height)*0.5; } break ; default : break ; } } } return textrect; } - ( void )drawtextinrect:(cgrect)requestedrect { cgrect actualrect = requestedrect; if (self.typearray) { actualrect = [self textrectforbounds:requestedrect limitedtonumberoflines:self.numberoflines]; } [super drawtextinrect:actualrect]; } - ( void )textalign:( void (^)(armaker *make))aligntype { armaker *make = [[armaker alloc]init]; aligntype(make); self.typearray = make.typearray; } @end //工具類 @implementation armaker - (instancetype)init { self = [super init]; if (self) { self.typearray = [nsmutablearray array]; } return self; } - (armaker *(^)( enum textaligntype type))addaligntype { __weak typeof (self) weakself = self; return ^( enum textaligntype type) { [weakself.typearray addobject:@(type)]; return weakself; }; } @end |
工具使用 - 九個方位對齊
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
- ( void )viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor whitecolor]; if (_index == 9) { //富文本底部對齊 [self attributedtextagainofbottom]; } else { aralignlabel *label = [[aralignlabel alloc] initwithframe:cgrectmake(kscreenwidth/2.0 - 150, 300, 300, 80)]; label.backgroundcolor = [uicolor orangecolor]; label.textcolor = [uicolor blackcolor]; label.font = [uifont systemfontofsize:18]; label.text = @ "愛學(xué)習(xí),愛編程,愛咖啡可樂" ; label.numberoflines = 1; [self.view addsubview:label]; switch (_index) { case 0: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_left).addaligntype(textaligntype_top); }]; break ; case 1: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_left).addaligntype(textaligntype_center); }]; break ; case 2: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_left).addaligntype(textaligntype_bottom); }]; break ; case 3: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_center).addaligntype(textaligntype_top); }]; break ; case 4: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_center); }]; break ; case 5: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_center).addaligntype(textaligntype_bottom); }]; break ; case 6: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_right).addaligntype(textaligntype_top); }]; break ; case 7: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_right).addaligntype(textaligntype_center); }]; break ; case 8: [label textalign:^(armaker *make) { make.addaligntype(textaligntype_right).addaligntype(textaligntype_bottom); }]; break ; default : break ; } } } |
富文本底部對齊
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
//富文本底部對齊 - ( void )attributedtextagainofbottom { cgfloat space = 10.0; aralignlabel *leftlb = [[aralignlabel alloc] initwithframe:cgrectmake(20, 200, kscreenwidth/2.0 - 20 - space/2.0, 80)]; leftlb.backgroundcolor = [uicolor lightgraycolor]; leftlb.textcolor = [uicolor blackcolor]; leftlb.numberoflines = 1; [self.view addsubview:leftlb]; //右下 [leftlb textalign:^(armaker *make) { make.addaligntype(textaligntype_center); }]; nsmutableattributedstring *attributedarr = [[nsmutableattributedstring alloc] initwithstring:@ "單價 $123" ]; [attributedarr setattributes:@{nsfontattributename:[uifont systemfontofsize:40], nsforegroundcolorattributename:[uicolor blackcolor]} range:nsmakerange(0, 1)]; [attributedarr setattributes:@{nsfontattributename:[uifont systemfontofsize:25], nsforegroundcolorattributename:[uicolor blackcolor]} range:nsmakerange(1, 1)]; [attributedarr setattributes:@{nsfontattributename:[uifont systemfontofsize:20], nsforegroundcolorattributename:[uicolor bluecolor]} range:nsmakerange(3, 1)]; [attributedarr setattributes:@{nsfontattributename:[uifont systemfontofsize:35], nsforegroundcolorattributename:[uicolor redcolor]} range:nsmakerange(4, attributedarr.length - 4)]; leftlb.attributedtext = attributedarr; //對齊之后 aralignlabel *rightlb = [[aralignlabel alloc] initwithframe:cgrectmake(kscreenwidth/2.0 + space/2.0, 200, leftlb.frame.size.width, 80)]; rightlb.backgroundcolor = [uicolor lightgraycolor]; rightlb.textcolor = [uicolor blackcolor]; rightlb.numberoflines = 1; [self.view addsubview:rightlb]; //左下 [rightlb textalign:^(armaker *make) { make.addaligntype(textaligntype_center); }]; //設(shè)置部分文字的偏移量 (0是讓文字保持原來的位置, 負值是讓文字下移,正值是讓文字上移) [attributedarr addattribute:nsbaselineoffsetattributename value:@(1) range:nsmakerange(0, 1)]; [attributedarr addattribute:nsbaselineoffsetattributename value:@(0) range:nsmakerange(1, 1)]; [attributedarr addattribute:nsbaselineoffsetattributename value:@(-2) range:nsmakerange(3, 1)]; [attributedarr addattribute:nsbaselineoffsetattributename value:@(-3) range:nsmakerange(4, attributedarr.length - 4)]; rightlb.attributedtext = attributedarr; } |
富文本底部對齊 - 使用場景:
github:https://github.com/archll/aruilabeltextalign
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.jianshu.com/p/ab1eb2785ec0