在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. Discuz常见小问题-如何批量加精,置顶帖子

    批量选中帖子,然后置顶和精华都可以勾选 完成之后的效果

  2. ObservableCollection<T> 类

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

  3. 微信小程序 - 日期(起止)选择器组件

    2019-01-03 : 修复了日期day-1,新增了年月日(除去时分秒),删除了不必要的touchmove 新增: column: ""(年月日) 配置: pickerConfi ...

  4. VCAP5-DCA – What’s new?

    see also: 韩国人的教材:http://ddii.pe.kr/ Section 1.1 – Implement and Manage complex storage Determine use ...

  5. Redis中取得所有Key、过期时间配置与获取、Key过期通知。

    string connection = "127.0.0.1:6379,password=xxxxx,connectRetry=5"; [TestMethod] public vo ...

  6. 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

    一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...

  7. java 反汇编class文件

      Created by Marydon on 1.情景展示 如何使用Java命令将字节码文件(class文件)反汇编? 2.解决方案 反汇编:将java文件编译后的class文件反汇编进而看到jav ...

  8. Xamarin.Android之SQLite.NET ORM

    一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之ContentProvider>的学习,我们已经掌握了如何 ...

  9. Android开发之将拍摄的图片传至服务器

    package com.example.oldtab; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...

  10. C++ UTF8和UTF16互转代码

    简介 1.这段代码只考虑在小端序情况下的转换(一般的机器都是的). 2.这段代码需要C++11的支持(只是用到了u16string),如果不支持,可以添加下面代码 typedef uint16_t c ...