一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - IOS - iOS開發實戰之Label全方位對齊的輕松實現

iOS開發實戰之Label全方位對齊的輕松實現

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

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

前言

本文主要給大家介紹了關于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

延伸 · 閱讀

精彩推薦
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5282021-01-16
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111312021-03-15
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
主站蜘蛛池模板: 亚洲香蕉网久久综合影院3p | 午夜神器老司机高清无码 | 99年水嫩漂亮粉嫩在线播放 | 护士柔佳 | 婷婷在线成人免费观看搜索 | 精品久久成人 | 日本高清免费中文字幕不卡 | 69老司机亚洲精品一区 | 经典千人斩一区二区视频 | 亚洲国产精品免费在线观看 | 国产福利片在线 易阳 | 免费网站看v片在线香蕉 | 日噜噜| 变态女王麻麻小说在线阅读 | 2019年国产高清情侣视频 | 青草热视频 | 91桃色视频 | 别停好爽好深好大好舒服视频 | 免费观看日本人成影片 | 性姿势女人嗷嗷叫图片 | 草久热 | 免费观看国产视频 | 国产欧美日韩不卡一区二区三区 | 国产免费小视频在线观看 | 香蕉精品高清在线观看视频 | 欧美兽皇video | 久久综合久综合久久鬼色 | 草莓视频旧版本 | 亚洲国产情侣偷自在线二页 | 亚洲精品福利一区二区在线观看 | 亚洲人成网站在线观看90影院 | 男女男精品网站 | 国产亚洲一区二区三区 | 99久久综合久中文字幕 | 特a级片| 国产精品女同久久免费观看 | 男人j放进女人的p视频免费 | 护士的小嫩嫩好紧好舒服 | 国产在线播放一区 | 91这里只有精品 | 2022色婷婷综合久久久 |