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

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

概述iOS开发实战之Label全方位对齐的轻松实现 前言 本文主要给大家介绍了关于iOS Label全方位对齐的实现方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 ARUILabelTextAlign 1. 实现 UILabel文本在 左(上 中 下).中(上 中 下).右(上 中 下) 9个方位显示: 2. 提供富文本底部不对齐的解决方案: 演示 核心代码: ARAlignLabel.h #import <UIKit/UIKit.h> @class ARMaker; typedef NS_ENUM(NSUInt ...

前言

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

ARUILabelTextAlign

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

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

演示

核心代码:

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,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:@"单价 3"]; [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,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;}

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

Github:https://github.com/ArchLL/ARUILabelTextAlign (本地下载)

总结

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

总结

以上是内存溢出为你收集整理的iOS开发实战之Label全方位对齐的轻松实现全部内容,希望文章能够帮你解决iOS开发实战之Label全方位对齐的轻松实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110048.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存