UI定制 -UILabel:自定义文本布局

自定义文本布局

1 - 代码示例:新建 UILabel 子类 UITextAlignLabel

 

// - UITextAlignLabel.h 

复制代码
 1 #import <UIKit/UIKit.h>
 2 @class AlignMaker;
 3 
 4 typedef NS_ENUM(NSUInteger,textAlignType){
 5     textAlignType_top = 10,    // 顶部对齐
 6     textAlignType_left,        // 居左对齐
 7     textAlignType_bottom,      // 底部对齐
 8     textAlignType_right,       // 居右对齐
 9     textAlignType_center       // 中心
10 };
11 
12 @interface UITextAlignLabel : UILabel
13 
14 -(void)textAlign: (void(^)(AlignMaker *make))alignType;// 根据设置的对齐方式进行文本对齐
15 
16 @end
17 
18 
19 @interface AlignMaker : NSObject
20 
21 // 存放对齐样式
22 @property(nonatomic,strong)NSMutableArray *typeArray;
23 
24 // 添加对齐样式
25 -(AlignMaker *(^)(textAlignType type))addAlignType;
26 
27 @end
复制代码

// - UITextAlignLabel.m

复制代码
  1 #import "UITextAlignLabel.h"
  2 @interface UITextAlignLabel ()
  3 
  4 @property(nonatomic,strong)NSArray *typeArray;// 对齐方式
  5 
  6 @property(nonatomic,assign)BOOL hasTop;     //
  7 @property(nonatomic,assign)BOOL hasLeft;    //
  8 @property(nonatomic,assign)BOOL hasBottom;  //
  9 @property(nonatomic,assign)BOOL hasRight;   //
 10 
 11 @end
 12 
 13 @implementation UITextAlignLabel
 14 
 15 - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
 16     CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
 17     if (self.typeArray){
 18 
 19         for (int i = 0; i < self.typeArray.count; i++) {
 20 
 21             textAlignType type = [self.typeArray[i] integerValue];
 22             switch (type) {
 23                     
 24                 // 顶部对齐
 25                 case textAlignType_top:
 26                     self.hasTop = YES;
 27                     textRect.origin.y = bounds.origin.y;
 28                     break;
 29 
 30                 // 居左对齐
 31                 case textAlignType_left:
 32                     self.hasLeft = YES;
 33                     textRect.origin.x = bounds.origin.x;
 34                     break;
 35 
 36                 // 底部对齐
 37                 case textAlignType_bottom:
 38                     self.hasBottom = YES;
 39                     textRect.origin.y = bounds.size.height - textRect.size.height;
 40                     break;
 41 
 42                 // 居右对齐
 43                 case textAlignType_right:
 44                     self.hasRight = YES;
 45                     textRect.origin.x = bounds.size.width - textRect.size.width;
 46                     break;
 47 
 48                 case textAlignType_center:
 49                     // 上中
 50                     if (self.hasTop) {
 51                         textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;
 52                     }
 53                     // 左中
 54                     else if (self.hasLeft) {
 55                         textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;
 56                     }
 57                     // 下中
 58                     else if (self.hasBottom) {
 59                         textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;
 60                     }
 61                     // 右中
 62                     else if (self.hasRight) {
 63                         textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;
 64                     }
 65                     // 上下左右居中
 66                     else{
 67                         textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;
 68                         textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;
 69                     }
 70                     break;
 71                 default:
 72                     break;
 73             }
 74         }
 75     }
 76 
 77     return textRect;
 78 }
 79 
 80 -(void)drawTextInRect:(CGRect)requestedRect {
 81     CGRect actualRect = requestedRect;
 82     if (self.typeArray) {
 83         actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
 84     }
 85     [super drawTextInRect:actualRect];
 86 }
 87 
 88 -(void)textAlign: (void(^)(AlignMaker *make))alignType{
 89 
 90     AlignMaker *make = [[AlignMaker alloc] init];
 91     alignType(make);
 92     self.typeArray = make.typeArray;
 93 }
 94 
 95 
 96 @end
 97 
 98 
 99 
100 #pragma mark - AlignMaker class
101 @implementation AlignMaker
102 
103 - (instancetype)init{
104     self = [super init];
105     if (self) {
106         self.typeArray = [NSMutableArray array];
107     }
108     return self;
109 }
110 
111 -(AlignMaker *(^)(enum textAlignType type))addAlignType{
112     __weak typeof (self) weakSelf = self;
113     return ^(enum textAlignType type){
114         [weakSelf.typeArray addObject:@(type)];
115         return weakSelf;
116     };
117 }
118 
119 @end
复制代码

2 - 具体使用

复制代码
 1 #import "ViewController.h"
 2 #import "UITextAlignLabel.h"
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11 
12     UITextAlignLabel *testLabel = [[UITextAlignLabel alloc] initWithFrame: CGRectMake(40, 20+60, self.view.frame.size.width - 80, 90)];
13     testLabel.text = @"还记得那年我们都还很年幼";
14     testLabel.backgroundColor = [UIColor orangeColor];
15     [self.view addSubview:testLabel];
16 
17     // 底部且居中
18     [testLabel textAlign:^(AlignMaker *make) {
19         
20         make.addAlignType(textAlignType_center).addAlignType(textAlignType_bottom);
21     }];
22 }
23 
24 
25 @end
复制代码

运行效果

 

posted on   低头捡石頭  阅读(42)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示