UILable的对齐方式(上对齐,下对齐,默认只能中间对齐)
2016-08-25 16:20
531 查看
.h文件
#import <UIKit/UIKit.h>
typedef enum
{
VerticalAlignmentTop,
VertiaclAlignmentBottom,
VerticalAlignmentMiddle
}VerticalAlignment;
@interface MyLable :
UILabel
@property(assign,nonatomic)
VerticalAlignment vertical;
@end
.m文件
#import "MyLable.h"
@implementation MyLable
-(void)setVertical:(VerticalAlignment)vertical
{
_vertical = vertical;
[self
setNeedsDisplay];
}
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect rect = [super
textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
switch (_vertical) {
case
VerticalAlignmentTop:
rect.origin.y = bounds.origin.y;
break;
case
VertiaclAlignmentBottom:
rect.origin.y = bounds.size.height - rect.size.height;
break;
default:
rect.origin.y = (bounds.size.height - rect.size.height)/2;
break;
}
return rect;
}
-(void)drawTextInRect:(CGRect)rect
{
CGRect rectt = [self
textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:rectt];
}
@end
#import <UIKit/UIKit.h>
typedef enum
{
VerticalAlignmentTop,
VertiaclAlignmentBottom,
VerticalAlignmentMiddle
}VerticalAlignment;
@interface MyLable :
UILabel
@property(assign,nonatomic)
VerticalAlignment vertical;
@end
.m文件
#import "MyLable.h"
@implementation MyLable
-(void)setVertical:(VerticalAlignment)vertical
{
_vertical = vertical;
[self
setNeedsDisplay];
}
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect rect = [super
textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
switch (_vertical) {
case
VerticalAlignmentTop:
rect.origin.y = bounds.origin.y;
break;
case
VertiaclAlignmentBottom:
rect.origin.y = bounds.size.height - rect.size.height;
break;
default:
rect.origin.y = (bounds.size.height - rect.size.height)/2;
break;
}
return rect;
}
-(void)drawTextInRect:(CGRect)rect
{
CGRect rectt = [self
textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:rectt];
}
@end
相关文章推荐
- 如何更改Java默认的花括号对齐方式
- eclipse默认的花括号对齐方式的修改
- UILable 设置对齐方式扩展
- IOS-55-UILable文字两边对齐的实现方式
- Xcode设置默认括号对齐方式_ultimateXin
- 在编写个人函数的时候,你将会受到C++中的一条基本的原则的限制:在默认的情况下,变量只能以值传递的方式传递给函数。这句话的意思是:被传递到函数的只是变量的值,永远不是变量的本身。
- phpexcel设置所有单元格的默认对齐方式
- 测试input,img,text混合的默认上下对齐方式
- #pragma pack()默认对齐方式
- C++11: nullptr、默认函数的控制、lambda函数、对齐方式
- UILabel的文字与 label 的中间线对齐方式
- 节点一复习-图片与文字对齐方式
- JS文件默认编码方式及修改
- 使用浏览器默认下载方式导出表格
- myeclise中设置默认为UTF-8的方式
- Flex中如何通过设置textAlign样式设定DateField中选中日期对齐方式的例子
- 更改mysql数据库默认的字符集(编码方式)
- 默认只能上传2M附件, 如何放开限制? (asp.net C#)
- jquery插件jquery.qrcode.js中使用二维码插件,中间放置logo图片的方式
- 修改tomcat默认的编码方式