博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS的UILabel设置居上对齐,居中对齐,居下对齐
阅读量:5022 次
发布时间:2019-06-12

本文共 3569 字,大约阅读时间需要 11 分钟。

前言:

没有理由不去努力.png

正文:

想实现UILabel居上对齐,居中对齐,居下对齐,如下效果:

效果图.png

在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐

具体如下:

创建:MYLabel 继承与UILabel

在MYLabel.h中完成

在MYLabel.h中完成

////  MYLabel.h//  LabelDemo//// Created by wangergang on 2016/12/7. // Copyright © 2016年 MYCompangName. All rights reserved. // #import 
typedef enum { VerticalAlignmentTop = 0, //default VerticalAlignmentMiddle, VerticalAlignmentBottom, } VerticalAlignment; @interface MYLabel : UILabel { @private VerticalAlignment _verticalAlignment; } @property (nonatomic) VerticalAlignment verticalAlignment; @end

在MYLabel.m中完成

在MYLabel.m中完成

////  MYLabel.m//  LabelDemo//// Created by wangergang on 2016/12/7. // Copyright © 2016年 MYCompangName. All rights reserved. // #import "MYLabel.h" @implementation MYLabel @synthesize verticalAlignment = verticalAlignment_; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.verticalAlignment = VerticalAlignmentMiddle; } return self; } - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment { verticalAlignment_ = verticalAlignment; [self setNeedsLayout]; } - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; switch (self.verticalAlignment) { case VerticalAlignmentTop: textRect.origin.y = bounds.origin.y; break; case VerticalAlignmentBottom: textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height; break; case VerticalAlignmentMiddle: // Fall through. default: textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0; } return textRect; } -(void)drawTextInRect:(CGRect)requestedRect { CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines]; [super drawTextInRect:actualRect]; } @end

使用:首先记得引入头文件

import "MYLabel.h"

- (void)viewDidLoad {    [super viewDidLoad];    [self setupLabel];    // Do any additional setup after loading the view, typically from a nib.}- (void)setupLabel { //居上对齐 MYLabel *topLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 275, 350, 200)]; topLabel.text = @"剧终了、剧终了、剧终了、剧终了、剧终了、剧终了、剧终了、"; topLabel.backgroundColor = [UIColor cyanColor]; topLabel.textAlignment = NSTextAlignmentLeft; topLabel.textColor = [UIColor blueColor]; topLabel.lineBreakMode = NSLineBreakByCharWrapping; topLabel.numberOfLines = 0; [topLabel setVerticalAlignment:VerticalAlignmentMiddle]; [self.view addSubview:topLabel]; //居中对齐 MYLabel *middleLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 500, 350, 200)]; middleLabel.text = @"向下看、向下看、向下看、向下看、向下看、向下看、向下看、向下看、"; middleLabel.backgroundColor = [UIColor cyanColor]; middleLabel.textAlignment = NSTextAlignmentLeft; middleLabel.textColor = [UIColor blueColor]; middleLabel.lineBreakMode = NSLineBreakByCharWrapping; middleLabel.numberOfLines = 0; [middleLabel setVerticalAlignment:VerticalAlignmentBottom]; [self.view addSubview:middleLabel]; //居下对齐 MYLabel *bottomLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 50, 350, 200)]; bottomLabel.text = @"看我居上对齐了啊、你看看对不对的啊、看来是对的"; bottomLabel.backgroundColor = [UIColor cyanColor]; bottomLabel.textAlignment = NSTextAlignmentLeft; bottomLabel.textColor = [UIColor blueColor]; bottomLabel.lineBreakMode = NSLineBreakByCharWrapping; bottomLabel.numberOfLines = 0; [bottomLabel setVerticalAlignment:VerticalAlignmentTop]; [self.view addSubview:bottomLabel]; }

其效果图如上图:就不再上传了

Demo位置

转载于:https://www.cnblogs.com/Free-Thinker/p/7053872.html

你可能感兴趣的文章
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>
graphite custom functions
查看>>
列出所有的属性键
查看>>
js获取请求地址后面带的参数
查看>>
[原创]使用java批量修改文件编码(ANSI-->UTF-8)
查看>>
设计模式のCompositePattern(组合模式)----结构模式
查看>>
二进制集合枚举子集
查看>>
磁盘管理
查看>>
SAS学习经验总结分享:篇二—input语句
查看>>
UIImage与UIColor互转
查看>>
RotateAnimation详解
查看>>
系统管理玩玩Windows Azure
查看>>
c#匿名方法
查看>>
如何判断链表是否有环
查看>>
【小程序】缓存
查看>>
ssh无密码登陆屌丝指南
查看>>