转载自:http://dong-zsh.github.io/2015/10/14/%E8%AE%BE%E7%BD%AElable%E5%86%85%E5%AE%B9%E4%B8%8D%E4%B8%8A%E4%B8%8B%E5%B1%85%E4%B8%AD/

#import <UIKit/UIKit.h>
typedef enum
{
VerticalAlignmentTop = 0, // default文字居上
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment; @interface mylable : UILabel {
@private
VerticalAlignment _verticalAlignment;
} @property (nonatomic) VerticalAlignment verticalAlignment; @end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "mylable.h"

@implementation mylable

@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];
}

###用法

1
2
3
4
5
6
mylable *lable = [[mylable alloc] initWithFrame:CGRectMake(10, 400, 100, 200)];
lable.text = @"dajdiojajdajdjajdadakdajdiaodaiojdioajdioajidjaijdajdadadna";
lable.backgroundColor = [UIColor grayColor];
lable.numberOfLines = 0;
[lable setVerticalAlignment:VerticalAlignmentTop];
[self.view addSubview:lable];

设置lable内容不上下居中的更多相关文章

  1. flutter textfield设置高度后内容区无法居中?

    textfiled 设置高度后,内容永远无法居中,最后找到原因 decoration: 中有一个 contentPadding属性,设置这个属性对应的Padding值即可

  2. 页面出现滚动条时,body里面的内容不能自动居中?

    弹窗后允许页面滚动 这种方式通常使用 position: absolute; ,可以看看我做的这个 Demo.主要用来应对弹窗内容很大很多的情况,超过了屏幕的宽高需要产生滚动条来方便浏览者查看.有一些 ...

  3. 使用MVVM DataTriggers在WPF XAML视图之间切换/Window窗口自适应内容大小并居中

    原文 使用MVVM DataTriggers在WPF XAML视图之间切换 相关文章: http://www.technical-recipes.com/2016/switching-between- ...

  4. js获取,设置FCKeditor内容

    // 获取编辑器中HTML内容 function getEditorHTMLContents(EditorName) {     var oEditor = FCKeditorAPI.GetInsta ...

  5. WP8.1开发中关于媒体(图片)文件的生成操作,属性如何设置(内容/嵌入资源等);

    (转载)WindowsPhone问题笔记-- 正确选择build action 解决媒体资源无法读取问题 链接:http://www.cnblogs.com/qinxg/archive/2012/07 ...

  6. Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)

    8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...

  7. 设置select,option文本居中

    设置select,option文本居中 可以通过 padding 属性设置内边距,使它看上去居中: select{ # 从左到右依次表示上内边距,右内边距,下内边距,左内边距: padding :0 ...

  8. jQuery学习-访问设置元素内容

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 设置div中的div居中显示

    设置div中的div居中显示 方法一. <div class='big'> <div class='small'>box1</div> </div> s ...

随机推荐

  1. SLua 中继承 C# 类接口 Slua.Class 的一个 Bug。

    由于目前要把大量的代码移植到 lua 中(真是够虐心的),面向对象肯定少不了,项目的代码都是这么设计的,于是就测试 Slua.Class 接口来扩展 C# 的类,发现有点问题,给作者提交了一个 Iss ...

  2. PHP中zlib扩展实现GZIP压缩输出各种方法总结

    一般情况下我们出现大量数据传输理希望减少服务器的带宽压力,会采取一种方式来压缩文件传输,php中用zlib也可以实现gzip压缩输出,下面我们来看GZIP压缩输出各种方法总结. GZIP(GNU-ZI ...

  3. Web开发,如何从小工到专家

    最近在研读关于“整体性学习”的一些东西,收获颇丰. 整体性学习强调的东西有三样:结构.模型.与高速通道.特别是关于结构的篇章: 理解是什么?理解就是结构高度发达完善的结果. 是不是有些学科你可以轻松“ ...

  4. 【转】谁说Vim不是IDE?(一)

    谁说Vim不是IDE?(一)   话说操作系统.编程语言和编辑器似乎是程序员永恒的吐槽话题,技术发展了几十年,大家讨论起这几个“之争”还是充满愤怒.津津乐道.有人描述过Emacs和VI程序员,大家沿着 ...

  5. hdoj 2074 叠筐

    叠筐 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  6. JAVA 调用Axis2 code generator 生成的webservice

    以下代码为调用 JAVA 调用Axis2 code generator 生成的webservice的代码. package test; import java.rmi.RemoteException; ...

  7. TM1668 Led 驱动芯片源程序

    #define P_1668DAT_In RA0 //数据输入端口 #define P_1668DAT LATA0 //数据输出端口 #define P_1668CLK LATA1 #define P ...

  8. 色情不是我的所有——在法律边缘起舞的 FC2

    [核心提示] 世界上最大的色情站点之中的一个这项「殊荣」只是是 FC2 无心插柳被用户戴上的一顶帽子,这个从 1999 年成立至今已经超过 15 年历史的互联网活化石,远比你想象中更加高深莫測. 一个 ...

  9. RichtextBox去除闪烁光标

    http://files.cnblogs.com/xe2011/CustomRichTextBox_HideCaret.rar richTextBox能高亮选择,光标仍在,没有光标闪烁 把重RichT ...

  10. [TypeScript] Using Typings and Loading From node_modules

    Using TypeScript when installing packages from npm often requires you to install related definition ...