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/UIKit.h>
- typedef enum
- {
- VerticalAlignmentTop = 0, // default
- VerticalAlignmentMiddle,
- VerticalAlignmentBottom,
- } VerticalAlignment;
- @interface myUILabel : UILabel
- {
- @private
- VerticalAlignment _verticalAlignment;
- }
- @property (nonatomic) VerticalAlignment verticalAlignment;
- @end
- //
- // myUILabel.m
- //
- //
- // Created by yexiaozi_007 on 3/4/13.
- // Copyright (c) 2013 yexiaozi_007. All rights reserved.
- //
- #import "myUILabel.h"
- @implementation myUILabel
- @synthesize verticalAlignment = verticalAlignment_;
- - (id)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.verticalAlignment = VerticalAlignmentMiddle;
- }
- return self;
- }
- - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
- verticalAlignment_ = verticalAlignment;
- [self setNeedsDisplay];
- }
- - (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
在使用时:
- lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];
- UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色
- lbl_mylabel.backgroundColor = color;
- lbl_mylabel.textAlignment = UITextAlignmentLeft;
- lbl_mylabel.textColor = UIColor.whiteColor;
- lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
- lbl_mylabel.numberOfLines = 0;
- [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
- [self addSubview:lbl_mylabel];
iOS UILabel设置居上对齐,居中对齐,居下对齐的更多相关文章
- dojo表格内容居左、居中和居右
1.常规表格内容居左.居中和居右 style="text-align:left;" style="text-align:center;" style=" ...
- iOS UILabel设置行间距和字间距
实现UILabel的文字,设置行间距和字间距. 效果图: 代码: let lblTitle = UILabel(frame: CGRect(x: , y: , width: KScreenWidth- ...
- IOS UIlabel设置文本距离边框距离
自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...
- iOS 给NSString文字上添加横线 中间和下划线
有时候我们需要给文字添加横线,有两种情况: 第一种是贯穿中间的横线: 横线的颜色和文字的颜色保持一致 _oldPriceLabel.text = "; _oldPriceLabel.text ...
- ckeditor5 使用第一天 下载并加载居中,居左,居右功能
官方网站地址https://ckeditor.com/,下载zip包或者从git上下载, 下载完成后解压文件,将文件复制到项目中 , 引用ckeditor.js,zh-cn.js路径到项目中, 初始化 ...
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐.具体如下: // // myUILabel.h ...
- Swift环境下实现UILabel居上 居中 居下对齐
首先在Xcode中新建.h文件,将下面代码复制进去 // // myUILabel.h // // // Created by yexiaozi_007 on 3/4/13. // Copyright ...
- css设置图片居中、居左、居右
CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...
- asp.net,CSS设置<TableListView>的title居左,居左,居上
居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...
随机推荐
- GetParam(name)
function GetParam(name) { var match = new RegExp(name + "=*([^&]+)*", "i").e ...
- .NET MVC中登陆授权过滤器的使用
1.写个类LoginAuthorityAttribute,继承自AuthorizeAttribute using System; using System.Collections.Generic; u ...
- Sql server中依据存储过程中的部分信息查找存储过程名称的方法【视图和Function】
1.查询的语句: select a.id,b.name,a.*,b.* from syscomments a join sysobjects b on a.id=b.id where b.xtype= ...
- 【转发】JQuery中操作Css样式的方法
JQuery中操作Css样式的方法 //1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#t ...
- cocos2d-js 小游戏 hungry-hero (原版是flash starling)
[本文被整理到cocos2d-js官方介绍中:http://cn.cocos2d-x.org/tutorial/show?id=1435] 之前看starling资料的时候,发现印度一高人的hungr ...
- SCD Type2 in ODI
缓慢变化维(Slowly changing Dimensions)指的是维表中的维度字段值会随着时间或业务调整,而在后续的分析中,历史数据仍然要使用旧的维度值,新的数据会使用当前维度值.在数据仓库建设 ...
- javascript 如何打印、输出数组内容(将数组内容以字符串的形式展示出来)
1.情景展示 在实际开发过程中,为了调试,有时我们需要看数组中具体包含了哪些数据,但是, 如果我们直接打印该数组对象,显示的结果却不是我们想要的. 如何才能将数组内容打印出来呢?(将数组内容以 ...
- 使用Maven Jenkins和JMeter自动化测试
有两个插件能够实现在Maven build集成Jmeter测试, jmeter-maven-plugin 和 chronos-maven-plugin. 选择哪一个取决于情况,下面是一些判断标准: ...
- RHEL7 配置临时IP 测试
RHEL7 配置或添加临时IP地址测试: [root@rhel7 Desktop]# ip a s enp0s3 ---查看原网卡IP 为192.168.1.7 : enp0s3: <BROAD ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...