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

2020-01-21 07:30:50王冬梅

前言

本文主要给大家介绍了关于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