欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-29 21:17:05
前言 本文主要给大家介绍了关于ios label全方位对齐的实现方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 aruilabeltextalig...

前言

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

aruilabeltextalign

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

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

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

演示

核心代码:

aralignlabel.h

#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

#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

工具使用 - 九个方位对齐

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

富文本底部对齐

//富文本底部对齐
- (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 (本地下载

总结

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