UILabel顶端对齐
比较一劳永逸的写法是对label添加一个分类
@interface UILabel (VerticalAlign)
/** 顶端对齐 */
-(void)alignTop;
/** 底部对齐 */
-(void)alignBottom;
@end
-(void)alignTop{
CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width;
CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=; i<newLinesToPad; i++){
self.text = [self.text stringByAppendingString:@"\n "];
}
}
-(void)alignBottom{
CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width;
CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=; i<newLinesToPad; i++){
self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
}
然后在使用时添加[myLabel alignTop]即可。
添加前

使用后

UILabel顶端对齐的更多相关文章
- UILabel顶部对齐解决方法(转载)
问题 我有一个UILabel高度最多能显示两行,如果里面内容只有一行,它是垂直居中的.怎么能让它顶端对齐呢? 回答 答案1:用sizeToFit改变UILabel的高度 nevan king,19 ...
- 两个div横向排列,顶端对齐的方式。
1.左右两个div都设置为float:left,如果右边div没有设置宽度,右边div的宽度会根据div里的内容自动调整. <!DOCTYPE html PUBLIC "-//W3C/ ...
- iOS UILabel两端对齐的实现(可包括中英文/数字)
- (void)conversionCharacterInterval:(NSInteger)maxInteger current:(NSString *)currentString withLabe ...
- iOS 中UI控件的各种对齐方式总结
1.textAligment : 文字的水平方向的对齐方式 取值 NSTextAlignmentLeft = 0, // 左对齐 NSTextAlignmentCenter = ...
- UILabel字体加粗等属性和特效
/* Accessing the Text Attributes text property font property textColor property textAlignment pr ...
- css对齐
2016-10-25 <css入门经典>第15章 1.text-align属性: 块属性内部的文本对齐方式.该属性只对块盒子有意义,内联盒子的内容没有对齐方式.(注意:只是盒子内部的内容对 ...
- vertical-align 属性设置元素的垂直对齐方式。
值 描述 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶 ...
- CSS 之 控制图片与文字对齐
文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明: baseline- ...
- css控制图片与文字对齐
文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明:baseline-将 ...
随机推荐
- JS-为金额添加千分位逗号分割符
前言:这个功能在前端页面中使用的还是比较多的,正好我们的项目中也有使用此功能,不过YY同学写的代码不像个方法的样子,一个入口中间又插了几道子,所 以,我写了下面这个方法,经过测 ...
- Dynamic V Strongly Typed Views
Come From https://blogs.msdn.microsoft.com/rickandy/2011/01/28/dynamic-v-strongly-typed-views/ There ...
- ssh 的搭建
struts包的下载:http://struts.apache.org/download.cgi#struts252 string包的下载: http://repo.spring.io/release ...
- Android IOS WebRTC 音视频开发总结(七八)-- 为什么WebRTC端到端监控很关键?
本文主要介绍WebRTC端到端监控(我们翻译和整理的,译者:weizhenwei,校验:blacker),最早发表在[编风网] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信I ...
- Shaders(读书笔记4 --- Real-Time rendering)
1. vertex,pixel以及geometry shaders共享一个programming model,即common-shader core,在GPU架构中的unified shader可以和 ...
- ARCGIS进行地理配准并加载到谷歌地球中查看
普通的地图图片如何能让其附有经纬度坐标和投影信息,如何能将普通的地图图片加载到诸如谷歌地球等相关的三维地球软件当中进行生产或学习使用呢,这就要用到gis当中常用的一种功能,叫做地理配准.地理配准并不复 ...
- python基础学习
1 list () 定义 2 dict() 转化为字典 3 tuple() 转化为元组 4 sort() 和 sorted()区别 5 a.sort(key=lambda ...
- 封装、调用ajax
1.JavaScript代码 //封装ajaxfunction ajax(obj) { var xhr = new createXHR(); obj.url = obj.url + '?rand=' ...
- c#判断闰年
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 使用C#的反射机制读取类的字段名称及值
using System.Windows.Forms;using System.Reflection; foreach (FieldInfo fi in typeof(SystemInformatio ...