比较一劳永逸的写法是对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顶端对齐的更多相关文章

  1. UILabel顶部对齐解决方法(转载)

    问题 我有一个UILabel高度最多能显示两行,如果里面内容只有一行,它是垂直居中的.怎么能让它顶端对齐呢?   回答 答案1:用sizeToFit改变UILabel的高度 nevan king,19 ...

  2. 两个div横向排列,顶端对齐的方式。

    1.左右两个div都设置为float:left,如果右边div没有设置宽度,右边div的宽度会根据div里的内容自动调整. <!DOCTYPE html PUBLIC "-//W3C/ ...

  3. iOS UILabel两端对齐的实现(可包括中英文/数字)

    - (void)conversionCharacterInterval:(NSInteger)maxInteger current:(NSString *)currentString withLabe ...

  4. iOS 中UI控件的各种对齐方式总结

    1.textAligment : 文字的水平方向的对齐方式 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NSTextAlignmentCenter    = ...

  5. UILabel字体加粗等属性和特效

    /* Accessing the Text Attributes text  property font  property textColor  property textAlignment  pr ...

  6. css对齐

    2016-10-25 <css入门经典>第15章 1.text-align属性: 块属性内部的文本对齐方式.该属性只对块盒子有意义,内联盒子的内容没有对齐方式.(注意:只是盒子内部的内容对 ...

  7. vertical-align 属性设置元素的垂直对齐方式。

     值 描述 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶 ...

  8. CSS 之 控制图片与文字对齐

    文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明: baseline- ...

  9. css控制图片与文字对齐

    文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明:baseline-将 ...

随机推荐

  1. javaEE中关于dao层和services层的理解

    javaEE中关于dao层和services层的理解 入职已经一个多月了,作为刚毕业的新人,除了熟悉公司的项目,学习公司的框架,了解项目的一些业务逻辑之外,也就在没学到什么:因为刚入职, 带我的那个师 ...

  2. 【原创】android内存管理-内存泄漏原因

    转载请注明出处 http://www.cnblogs.com/weiwangnuanyang/p/5704596.html 先讲一下内存泄漏的概念:内存泄露是指无用对象持续占有内存,或者内存得不到及时 ...

  3. .NET (五)委托第五讲:内置委托Predicate

    // 摘要: // 表示定义一组条件并确定指定对象是否符合这些条件的方法. // // 参数: // obj: // 要按照由此委托表示的方法中定义的条件进行比较的对象. // // 类型参数: // ...

  4. a different object with the same identifier value was already associated with the session:错误;

    当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...

  5. spring 事务问题

    今天碰到一个奇怪的问题,在service中执行方法,调用了两次dao,前面是save,在save后面抛错,竟然没回滚,难道不是一个事务? 后来网上查资料,发现spring的事务回滚必须是运行时异常Ru ...

  6. js中各种事件的兼容性

    1给元素绑定一个事件 ie8attachEvent(on+"事件",event); 高级浏览器 addEventListener("事件",event,fals ...

  7. yii + elasticsearch 手册

    https://zhuowenji1.gitbooks.io/elasticsearch/content/an_zhuang_yii2.html

  8. jmeter性能测试小小的实战

    项目描述: 被测网站:xqtesting.blog.51cto.com 指标:响应时间以及错误率 场景: 新建 一个线程组,设置线程属性: 线程数为10 Ramp-Up-Penod(in second ...

  9. const char **

    foo (const char **p){    }   main (int argh,char **argv) {      foo(argv); } warning : argument is i ...

  10. 通用Hibernate DAO类(包括分页)

    package com.jronline.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.S ...