在iOS中默认的UILabel中的文字在竖直方向上仅仅能居中对齐,博主參考国外站点。从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。详细例如以下:

  1. //
  2. //  myUILabel.h
  3. //
  4. //
  5. //  Created by yexiaozi_007 on 3/4/13.
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. typedef enum
  10. {
  11. VerticalAlignmentTop = 0, // default
  12. VerticalAlignmentMiddle,
  13. VerticalAlignmentBottom,
  14. } VerticalAlignment;
  15. @interface myUILabel : UILabel
  16. {
  17. @private
  18. VerticalAlignment _verticalAlignment;
  19. }
  20. @property (nonatomic) VerticalAlignment verticalAlignment;
  21. @end
  1. //
  2. //  myUILabel.m
  3. //
  4. //
  5. //  Created by yexiaozi_007 on 3/4/13.
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.
  7. //
  8. #import "myUILabel.h"
  9. @implementation myUILabel
  10. @synthesize verticalAlignment = verticalAlignment_;
  11. - (id)initWithFrame:(CGRect)frame {
  12. if (self = [super initWithFrame:frame]) {
  13. self.verticalAlignment = VerticalAlignmentMiddle;
  14. }
  15. return self;
  16. }
  17. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
  18. verticalAlignment_ = verticalAlignment;
  19. [self setNeedsDisplay];
  20. }
  21. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
  22. CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
  23. switch (self.verticalAlignment) {
  24. case VerticalAlignmentTop:
  25. textRect.origin.y = bounds.origin.y;
  26. break;
  27. case VerticalAlignmentBottom:
  28. textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
  29. break;
  30. case VerticalAlignmentMiddle:
  31. // Fall through.
  32. default:
  33. textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
  34. }
  35. return textRect;
  36. }
  37. -(void)drawTextInRect:(CGRect)requestedRect {
  38. CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
  39. [super drawTextInRect:actualRect];
  40. }
  41. @end

在使用时:

  1. lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];
  2. UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色
  3. lbl_mylabel.backgroundColor = color;
  4. lbl_mylabel.textAlignment = UITextAlignmentLeft;
  5. lbl_mylabel.textColor = UIColor.whiteColor;
  6. lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
  7. lbl_mylabel.numberOfLines = 0;
  8. [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
  9. [self addSubview:lbl_mylabel];

iOS UILabel设置居上对齐,居中对齐,居下对齐的更多相关文章

  1. dojo表格内容居左、居中和居右

    1.常规表格内容居左.居中和居右 style="text-align:left;" style="text-align:center;" style=" ...

  2. iOS UILabel设置行间距和字间距

    实现UILabel的文字,设置行间距和字间距. 效果图: 代码: let lblTitle = UILabel(frame: CGRect(x: , y: , width: KScreenWidth- ...

  3. IOS UIlabel设置文本距离边框距离

    自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...

  4. iOS 给NSString文字上添加横线 中间和下划线

    有时候我们需要给文字添加横线,有两种情况: 第一种是贯穿中间的横线: 横线的颜色和文字的颜色保持一致 _oldPriceLabel.text = "; _oldPriceLabel.text ...

  5. ckeditor5 使用第一天 下载并加载居中,居左,居右功能

    官方网站地址https://ckeditor.com/,下载zip包或者从git上下载, 下载完成后解压文件,将文件复制到项目中 , 引用ckeditor.js,zh-cn.js路径到项目中, 初始化 ...

  6. iOS的UILabel设置居上对齐,居中对齐,居下对齐

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

  7. Swift环境下实现UILabel居上 居中 居下对齐

    首先在Xcode中新建.h文件,将下面代码复制进去 // // myUILabel.h // // // Created by yexiaozi_007 on 3/4/13. // Copyright ...

  8. css设置图片居中、居左、居右

      CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...

  9. asp.net,CSS设置<TableListView>的title居左,居左,居上

    居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...

随机推荐

  1. .NET 托管、非托管、本地:这些代码有什么区别?

    http://www.codeguru.com/Csharp/.NET/cpp_managed/article.php/c4871 本文内容 什么是托管代码? 什么是非托管代码? 什么是本地代码? 托 ...

  2. powerdesigner 生成数据库脚本

    PowerDesigner导出所有SQL脚本 操作:Database=>Generate Database PowerDesigner怎么导出建表sql脚本 1 按照数据库类型,切换数据库. D ...

  3. ObservableCollection<T> 类

    命名空间: System.Collections.ObjectModel程序集: System(在 System.dll 中)XAML 的 XMLNS: 未映射到 xmlns. 添加:using Sy ...

  4. linux 查看网线断开 网卡是否关闭

    linux 查看网线断开 网卡是否关闭 探测是否存在网络接口:  SIOCGIFFLAGS

  5. C++ 第六课:C/C++关键字及其用法

    asm 插入一个汇编指令. auto 声明一个本地变量. bool 声明一个布尔型变量. break 结束一个循环. case 一个switch语句的一部分. catch 处理 thrown 产生的异 ...

  6. python获取系统开机时间

    import psutil import time time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))

  7. 很赞的idea教程

    感谢: http://pan.baidu.com/s/1dDEaVxn

  8. spring mvc防止表单重复提交的代码片段

    1.定义一个token接口 package com.bigbigrain.token; import java.lang.annotation.Documented; import java.lang ...

  9. cocos2d-js Shader系列1:cocos2d-js Shader和OpenGL ES2.0

    cocos2d的Shader也就是差不多直接跟GPU打交道了,跟Flash的Stage3D(AGAL)类似,不过没有AGAL这么恶心,不需要直接编写汇编语言.而Fragment Shader又跟Fla ...

  10. python 模块之platform模块(基本了解)

    # -*- coding: cp936 -*-#python 27#xiaodeng#python 模块之platform模块(基本了解)#获取底层平台的识别数据 #知道该模块用途即可,需要使用时直接 ...