iOS开发实战之Label全方位对齐的轻松实现

时间:2021-08-16 20:09:44

前言

本文主要给大家介绍了关于ios label全方位对齐的实现方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

aruilabeltextalign

1. 实现 uilabel文本在 左(上 中 下)、中(上 中 下)、右(上 中 下) 9个方位显示;

2. 提供富文本底部不对齐的解决方案;

iOS开发实战之Label全方位对齐的轻松实现

演示

核心代码:

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
 
/**
 * 根据对齐方式进行文本对齐
 *
 * @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 = @"爱学习,爱编程,爱咖啡可乐";
  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);
 }];
 
 //设置部分文字的偏移量 (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;
 
}

富文本底部对齐 - 使用场景:

iOS开发实战之Label全方位对齐的轻松实现

github:https://github.com/archll/aruilabeltextalign

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/ab1eb2785ec0