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

前言

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

}

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

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

总结

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

(0)

相关推荐

  • iOS中UILabel text两边对齐的实现代码

    废话不多说了,直接给大家贴代码了,具体代码如下所示: -(NSAttributedString *)setTextString:(NSString *)text { NSMutableAttributedString *mAbStr = [[NSMutableAttributedString alloc] initWithString:text]; NSMutableParagraphStyle *npgStyle = [[NSMutableParagraphStyle alloc] init]

  • iOS中UILabel设置居上对齐、居中对齐、居下对齐及文字置顶显示

    iOS中UILabel设置居上对齐.居中对齐.居下对齐 在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐. 具体如下: // // myUILabel.h // // // Created by yexiaozi_007 on 3/4/13. // Copyright (c) 2013 yexiaozi_007. All rights reserved. // #import <UIKit/UIK

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

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

  • iOS开发之表视图详解

    本文详细介绍了表视图的用法.具体如下: 概述 表视图组成 表视图是iOS开发中最重要的视图,它以列表的形式展示数据.表视图又一下部分组成: 表头视图:表视图最上边的视图 表脚视图:表视图最下边的视图 单元格(cell):表视图中每一行的视图 节(section):由多个单元格组成,应用于分组列表 节头 节脚 表视图的相关类 UITableView继承自UIScrollView,且有两个协议:UITableViewDelegate和UITableViewDataSource.此外UITableVi

  • JavaScript使用DeviceOne开发实战(二) 生成调试安装包

    在上篇文章给大家介绍了JavaScript使用DeviceOne开发实战(一) 配置和起步,本篇文章继续给大家介绍关于javascript实战相关内容,一起学习吧. 生成调试安装包 首先需要说明的是,这个步骤并不是每次调试App都必须的,大部分情况生成一次调试安装包,安装到手机上之后就可以忽略整个这个步骤.因为调试安装包包含了很多原生组件,都是可以定制勾选的,如果你需要额外增加一些原生组件,则需要勾选更多的组件并要重新生成调试安装包. 点击调试程序的菜单里的"Build Debug Versio

  • JavaScript使用DeviceOne开发实战(一) 配置和起步

    2015 年 9 月 底,DeviceOne Release发布.至此,DeviceOne 基本完成了对多端的支持.基于 DeviceOne 可以: HTML5.Android.iOS.Windows 多端代码一次编写,各处复用: 实时简单部署. 本地化UI 在接下来的时间,我会通过一系列文章来介绍 DeviceOne.本文介绍环境配置以及如何建立一个简单的项目.(注:本篇文章 iOS 和 Android和Windows 开发都适用.) 目前使用 DeviceOne 开发可以在Windows 或

  • iOS开发总结之UILabel常用属性介绍

    1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text]; [textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UICol

  • iOS开发笔记之键盘、静态库、动画和Crash定位

    前言 本文主要分享了开发中遇到的问题,和相关的一些思考.分享出来给有需要的朋友们参考学习,下面话不多说了,来一起看看详细的介绍吧. iOS11键盘问题 功能背景: 弹出键盘时,如果有输入框的话,需要输入框的位置跟随键盘大小而变动. 问题描述: 当快速切换键盘之后,容易出现输入框的位置没有紧贴键盘,如下:(以简书键盘为例) 相关实现: 输入框监听系统的UIKeyboardWillShowNotification和UIKeyboardWillHideNotification事件,在回调的过程中用UI

  • iOS开发中使用UILabel设置字体的相关技巧小结

    一.初始化 复制代码 代码如下: UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];      [self.view addSubview:myLabel]; 二.设置文字 1.设置默认文本 复制代码 代码如下: NSString *text = @"标签文本"; myLabel.text = text; 效果: 2.设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不

  • React Native搭建iOS开发环境

    一.写在前面 1. 什么是React-Native? React-Native是:Facebook 在2015年初React.js技术研讨大会上公布的一个开源项目.支持用开源的JavaScript库React.js来开发iOS和Android原生App.初期仅支持iOS平台,同年9月份,该开源项目同时支持Android平台. React Native的原理是:在JavaScript中用React抽象操作系统原生的UI组件,代替DOM元素来渲染,比如以<View>取代<div>,以&

  • IOS 开发之读取addressbook的实现实例

    IOS 开发之读取addressbook的实现实例 iphone读取addressbook: 1.添加addressbook.framework 2.在需要的源文件中     #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 3.开始粘代码: //get all people info from the address book ABAddressBookRef addressBo

  • iOS开发中使用UIScrollView实现无限循环的图片浏览器

    一.概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件都介绍一遍确实没有必要,所谓授人以鱼不如授人以渔,这里会尽可能让大家明白其中的原理,找一些典型的控件进行说明,这样一来大家就可以触类旁通.今天我们主要来看一下UIScrollView的内容: UIView UIScrollView 实战--图片浏览器 二.UIView 在熟悉UIScrollView之前很有必要说一下UIView的内容.

随机推荐