UILabel设置顶、左、右、底部对齐
来源:互联网 发布:飞跃车主采集软件 编辑:程序博客网 时间:2024/06/10 17:06
1、主要用到了两个方法和一个工具类ZWMaker:
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
-(void)drawTextInRect:(CGRect)requestedRect
2、对齐类型:
typedef NS_ENUM(NSUInteger,textAlignType){ textAlignType_top = 10, //顶部对齐 textAlignType_left, //左边对齐 textAlignType_bottom, //底部对齐 textAlignType_right, //右边对齐 textAlignType_center //水平/垂直对齐(默认中心对齐)};
3、创建一个ZWVerticalAlignLabel继承UILabel,在ZWVerticalAlignLabel中重写这两个方法,根据对齐类型分别进行设置。
主要代码:
ZWVerticalAlignLabel.m
- (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];}
调用方法:
[label textAlign:^(ZWMaker *make) { make.addAlignType(textAlignType_bottom).addAlignType(textAlignType_right); }];
- make.addAlignType(textAlignType_bottom); //底部对齐
- make.addAlignType(textAlignType_top); //顶部对齐
- make.addAlignType(textAlignType_left); //左端对齐
- make.addAlignType(textAlignType_right); //右端对齐
- make.addAlignType(textAlignType_top).addAlignType(textAlignType_center); //顶部中间对齐
- ….
效果图:
项目地址:https://github.com/MisterZhouZhou/UILabelTextAlign
0 0
- UILabel设置顶、左、右、底部对齐
- CSS各种对齐代码,左对齐、右对齐、中间对齐、底部对齐、两端对齐等,代码简单
- CSS各种对齐代码,左对齐、右对齐、中间对齐、底部对齐、两端对齐等,代码简单
- UILabel的顶对齐解决方法
- 左对齐和右对齐
- vba设置Excel单元格左对齐、右对齐、居中对齐、字体、字号、字型
- 设置UILabel 两端对齐
- 乘法口诀右对齐和左对齐
- word左边左对齐,右边右对齐
- c++中关于设置左对齐和右对齐的问题解决方法
- 设置顶到状态栏底部
- 设置select右对齐
- IOS UILabel垂直顶部,中间,底部对齐
- 实现字符串的对齐(左对齐、右对齐、居中)
- ext toolbar 里的button默认都是靠左对齐的,设置靠右
- shell 右对齐左空格例子
- Android Toolbar左、中、右对齐
- UILabel对齐方式 距上 居左 对齐
- 深入理解FIFO(包含有FIFO深度的解释)
- HelloWorld帧速率
- 修改数组大小扩充数组
- [乐意黎]学驾照自动档之宝来灯光
- C++第四次实验
- UILabel设置顶、左、右、底部对齐
- Android 通过USB查看kernel调试信息
- 面向接口编程之二——编程实例
- SAP STO小解
- Dialog
- RS485波特率问题
- 解压(.rar及.zip格式)压缩文件
- html手机端 解决滑动跟点击的冲突问题
- linux多线程服务器编程 muduo库学习笔记