iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
来源:互联网 发布:mac补丁 for vm10 编辑:程序博客网 时间:2024/04/28 17:28
iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
VerticalCenterTextLabel.h
#import <UIKit/UIKit.h>typedef enum : NSUInteger { myVerticalAlignmentNone = 0, myVerticalAlignmentCenter, myVerticalAlignmentTop, myVerticalAlignmentBottom} myVerticalAlignment;@interface VerticalCenterTextLabel : UILabel@property (nonatomic) UIEdgeInsets edgeInsets;/** * 对齐方式 */@property (nonatomic) myVerticalAlignment verticalAlignment;@end
VerticalCenterTextLabel.m
#import "VerticalCenterTextLabel.h"@implementation VerticalCenterTextLabel- (void)setVerticalAlignment:(myVerticalAlignment)verticalAlignment{ _verticalAlignment= verticalAlignment; [self setNeedsDisplay];}- (void)drawTextInRect:(CGRect)rect{ if (_verticalAlignment == myVerticalAlignmentNone) { [super drawTextInRect:UIEdgeInsetsInsetRect(self.bounds, self.edgeInsets)]; } else { CGRect textRect = [self textRectForBounds:UIEdgeInsetsInsetRect(rect, self.edgeInsets) limitedToNumberOfLines:self.numberOfLines]; [super drawTextInRect:textRect]; }}- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{ CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; switch (_verticalAlignment) { case myVerticalAlignmentTop: textRect.origin.y = bounds.origin.y; break; case myVerticalAlignmentBottom: textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height; break; case myVerticalAlignmentCenter: textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0; break; default: break; } return textRect;}@end
有问题请联系博主,邮箱:bestnathan@aliyun.com
著作权声明:本文由http://www.bestnathan.com/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
0 0
- iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
- iOS UILabel垂直居中
- 使UILabel垂直靠上对起
- 【IOS实战】UIButton 设置图片文字垂直居中排列
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- Android中TextView文字居中与垂直靠左居中
- iOS UIButton 设置图片文字垂直排列
- iOS UIButton 设置图片文字垂直排列
- css布局小技巧之:让一个小图片水平靠右对齐,垂直居中
- UIbutton 上设置文字图片垂直居中对齐
- UITextView设置文字垂直居中和LinkAttribute
- UIButton 设置图片文字垂直居中排列
- 关于文字垂直居中的设置
- 设置字体垂直居中
- CSS 垂直居中设置
- android - See Build Numbers for a full listing of branches you may choose from
- 【待重置】堆排序
- dmp文件导入oracle
- 一维动态规划和二维动态规划中两道经典题目
- Project Israfil -- 支持多个音乐平台的开源音乐服务
- iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
- 在eclipse下使用Maven创建Struts2项目样例
- Qt:多线程
- python轻量级爬虫学习笔记 之 urllib的应用
- (七)JavaScript string对象及方法
- LeetCode-110.Balanced Binary Tree
- Path Sum
- tcp数据报头解析
- ASCLL码表的区域划分