Swift环境下实现UILabel居上 居中 居下对齐
来源:互联网 发布:中级java工程师薪资 编辑:程序博客网 时间:2024/05/22 16:43
首先在Xcode中新建.h文件,将以下代码复制进去
//// 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{@privateVerticalAlignment _verticalAlignment;}@property (nonatomic) VerticalAlignment verticalAlignment;@end
再新建一个.m文件,拷入以下代码
//// 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
如果这是你导入的第一个.m文件Xcode会提示你要不要创建Bridging-Header,选Ok
在新创建的Bridging-Header文件里拷入下方代码
#import "myUILabel.h"
然后右键拖动Label或者按住Control键左键拖动连线到Label所在的父View的Class中生成Outlet,如果之前已经连线好,则改完Custom Class后,将连线生成代码中的UILabel改为myUILabel,示意图如下
然后就可以调用该label的类方法
label.verticalAlignment = VerticalAlignmentBottom
按上方代码可以实现居下对其,居中 居上 分别将代码中的Bottom改为Middle和Top,默认为居上
5 3
- Swift环境下实现UILabel居上 居中 居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- ios UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS UILabel设置居上对齐,居中对齐,居下对齐
- iOS UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- 自定义UILabel,具有居上/居下/居中的功能
- UILable属性详解,设置居上对齐,居中对齐,居下对齐,获取斜体字,字体、大小、单位、颜色
- UILabel对齐方式 距上 居左 对齐
- UILabel textAlignment 居中 左右对齐
- Swift autoLayout 下的UILabel 换行问题
- 绝对定位下的居中对齐
- iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
- 三种继承方式的区别
- OpenGL 获取当前屏幕坐标对应的三维坐标,使用很简单glu库中的一个函数
- 原来如此
- kali 2.0安装VM tools
- Oracle学习笔记20150818一些常见查询语句
- Swift环境下实现UILabel居上 居中 居下对齐
- 软件汉化
- DAO设计模式
- GDOI2016模拟8.16逃离牛棚
- Android侧滑菜单
- SparkR
- hibernate 保存图片到数据库(oracle)
- 《Java网络编程》读书笔记(一)
- Linux源代码目录树结构