iOS UILabel设置居上对齐,居中对齐,居下对齐
在iOS中默认的UILabel中的文字在竖直方向上仅仅能居中对齐,博主參考国外站点。从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。详细例如以下:
- //
- // myUILabel.h
- //
- //
- // Created by yexiaozi_007 on 3/4/13.
- // Copyright (c) 2013 yexiaozi_007. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- typedef enum
- {
- VerticalAlignmentTop = 0, // default
- VerticalAlignmentMiddle,
- VerticalAlignmentBottom,
- } VerticalAlignment;
- @interface myUILabel : UILabel
- {
- @private
- VerticalAlignment _verticalAlignment;
- }
- @property (nonatomic) VerticalAlignment verticalAlignment;
- @end
- //
- // myUILabel.m
- //
- //
- // Created by yexiaozi_007 on 3/4/13.
- // Copyright (c) 2013 yexiaozi_007. All rights reserved.
- //
- #import "myUILabel.h"
- @implementation myUILabel
- @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];
- }
- @end
在使用时:
- lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];
- UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色
- lbl_mylabel.backgroundColor = color;
- lbl_mylabel.textAlignment = UITextAlignmentLeft;
- lbl_mylabel.textColor = UIColor.whiteColor;
- lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
- lbl_mylabel.numberOfLines = 0;
- [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
- [self addSubview:lbl_mylabel];
iOS UILabel设置居上对齐,居中对齐,居下对齐的更多相关文章
- dojo表格内容居左、居中和居右
1.常规表格内容居左.居中和居右 style="text-align:left;" style="text-align:center;" style=" ...
- iOS UILabel设置行间距和字间距
实现UILabel的文字,设置行间距和字间距. 效果图: 代码: let lblTitle = UILabel(frame: CGRect(x: , y: , width: KScreenWidth- ...
- IOS UIlabel设置文本距离边框距离
自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...
- iOS 给NSString文字上添加横线 中间和下划线
有时候我们需要给文字添加横线,有两种情况: 第一种是贯穿中间的横线: 横线的颜色和文字的颜色保持一致 _oldPriceLabel.text = "; _oldPriceLabel.text ...
- ckeditor5 使用第一天 下载并加载居中,居左,居右功能
官方网站地址https://ckeditor.com/,下载zip包或者从git上下载, 下载完成后解压文件,将文件复制到项目中 , 引用ckeditor.js,zh-cn.js路径到项目中, 初始化 ...
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐.具体如下: // // myUILabel.h ...
- Swift环境下实现UILabel居上 居中 居下对齐
首先在Xcode中新建.h文件,将下面代码复制进去 // // myUILabel.h // // // Created by yexiaozi_007 on 3/4/13. // Copyright ...
- css设置图片居中、居左、居右
CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...
- asp.net,CSS设置<TableListView>的title居左,居左,居上
居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...
随机推荐
- Discuz常见小问题-如何批量加精,置顶帖子
批量选中帖子,然后置顶和精华都可以勾选 完成之后的效果
- ObservableCollection<T> 类
命名空间: System.Collections.ObjectModel程序集: System(在 System.dll 中)XAML 的 XMLNS: 未映射到 xmlns. 添加:using Sy ...
- 微信小程序 - 日期(起止)选择器组件
2019-01-03 : 修复了日期day-1,新增了年月日(除去时分秒),删除了不必要的touchmove 新增: column: ""(年月日) 配置: pickerConfi ...
- VCAP5-DCA – What’s new?
see also: 韩国人的教材:http://ddii.pe.kr/ Section 1.1 – Implement and Manage complex storage Determine use ...
- Redis中取得所有Key、过期时间配置与获取、Key过期通知。
string connection = "127.0.0.1:6379,password=xxxxx,connectRetry=5"; [TestMethod] public vo ...
- 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...
- java 反汇编class文件
Created by Marydon on 1.情景展示 如何使用Java命令将字节码文件(class文件)反汇编? 2.解决方案 反汇编:将java文件编译后的class文件反汇编进而看到jav ...
- Xamarin.Android之SQLite.NET ORM
一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之ContentProvider>的学习,我们已经掌握了如何 ...
- Android开发之将拍摄的图片传至服务器
package com.example.oldtab; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...
- C++ UTF8和UTF16互转代码
简介 1.这段代码只考虑在小端序情况下的转换(一般的机器都是的). 2.这段代码需要C++11的支持(只是用到了u16string),如果不支持,可以添加下面代码 typedef uint16_t c ...