国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

2021-05-13 18:14Metro追光者 IOS

這篇文章主要給大家介紹了關(guān)于iOS開發(fā)實戰(zhàn)之輕松實現(xiàn)Label全方位對齊的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

本文主要給大家介紹了關(guān)于ios label全方位對齊的實現(xiàn)方法,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧

aruilabeltextalign

1. 實現(xiàn) uilabel文本在 左(上 中 下)、中(上 中 下)、右(上 中 下) 9個方位顯示;

2. 提供富文本底部不對齊的解決方案;

iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

演示

核心代碼:

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;
 
}

富文本底部對齊 - 使用場景:

iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜电影网址 | 欧美日韩视频在线 | 高清在线一区 | 免费看黄色一级电影 | 操操爱| 欧美一级在线 | 自拍偷拍一区二区三区 | 3d动漫精品一区二区三区 | 黄视频网站免费观看 | 久久精品亚洲成在人线av网址 | 日韩视频在线一区二区 | 黄色毛片视频网站 | 日本一区二区在线播放 | 一本一本久久a久久精品综合妖精 | 久久不卡| 三级国产网站 | 91精品国产91久久久久久黑人 | 国产三级久久久久 | 一区二区国产在线观看 | 欧美日韩国产免费 | 欧美国产精品一区二区三区 | 成人精品在线视频 | 午夜影晥 | 鲁管视频| 亚洲视频二区 | 精品国产成人 | 欧美在线视频网 | 毛片黄片 | 免费观看黄色大片 | 久久综合成人精品亚洲另类欧美 | 亚洲自拍中文 | 国产精品jizz在线观看麻豆 | 精品一区二区三区免费毛片爱 | www.久久精品 | 国产亚洲精品精品国产亚洲综合 | 色888www视频在线观看 | 国内外成人激情免费视频 | 国产亚洲精 | 毛片一区 | 国产精品18久久久 | 日韩中文字幕一区 |