3种方法实现UILabel的左上角对齐显示文字
2016-08-19 15:01
393 查看
1、继承UILabel 改变origin的值重新绘制
@interface TopLeftLabel :
UILabel
@end
#import
"TopLeftLabel.h"
@implementation TopLeftLabel
- (id)initWithFrame:(CGRect)frame {
return [super
initWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect textRect = [super
textRectForBounds:bounds
limitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
// 重新绘制text的位置
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self
textRectForBounds:requestedRect
limitedToNumberOfLines:self.numberOfLines];
[super
drawTextInRect:actualRect];
}
@end
2、第二种方法 写一个UILabel的分类
@interface UILabel (LeftTopAlign)
- (void) textLeftTopAlign;
@end
@implementation UILabel (LeftTopAlign)
- (void) textLeftTopAlign
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]init];
paragraphStyle.lineBreakMode =
NSLineBreakByWordWrapping;
NSDictionary *attributes =
@{NSFontAttributeName:[UIFont
systemFontOfSize:17],
NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [self.text
boundingRectWithSize:CGSizeMake(207,
999)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
CGRect dateFrame =CGRectMake(2,
140,
CGRectGetWidth(self.frame)-5,
labelSize.height);
self.frame = dateFrame;
}
@end
3、直接用UITextView代替 UILabel 然后禁用UITextView 的滚动效果
@interface TopLeftLabel :
UILabel
@end
#import
"TopLeftLabel.h"
@implementation TopLeftLabel
- (id)initWithFrame:(CGRect)frame {
return [super
initWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect textRect = [super
textRectForBounds:bounds
limitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
// 重新绘制text的位置
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self
textRectForBounds:requestedRect
limitedToNumberOfLines:self.numberOfLines];
[super
drawTextInRect:actualRect];
}
@end
2、第二种方法 写一个UILabel的分类
@interface UILabel (LeftTopAlign)
- (void) textLeftTopAlign;
@end
@implementation UILabel (LeftTopAlign)
- (void) textLeftTopAlign
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]init];
paragraphStyle.lineBreakMode =
NSLineBreakByWordWrapping;
NSDictionary *attributes =
@{NSFontAttributeName:[UIFont
systemFontOfSize:17],
NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [self.text
boundingRectWithSize:CGSizeMake(207,
999)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
CGRect dateFrame =CGRectMake(2,
140,
CGRectGetWidth(self.frame)-5,
labelSize.height);
self.frame = dateFrame;
}
@end
3、直接用UITextView代替 UILabel 然后禁用UITextView 的滚动效果
相关文章推荐
- CSS控制图片和文字在同一行显示且对齐的3种方法
- CSS控制图片和文字在同一行显示且对齐的3种方法
- CSS控制图片和文字在同一行显示且对齐的3种方法
- 实现两个不同高度的UILabel的文字底部相对于下划线对齐。不是很简单吗? 是吗?你敢按你常规方法试试让你那火眼金睛的设计、美工去瞧瞧???
- CSS控制图片和文字在同一行显示且对齐的3种方法
- css实现两端对齐的3种方法
- css实现文字过长显示省略号的方法
- JS简单实现点击按钮或文字显示遮罩层的方法
- css 让同一行的图片和文字对齐的3种方法
- css实现文字指定位置显示,底部对齐,文字在块内的垂直对齐
- css实现两端对齐的3种方法
- html里,实现图片与文字对齐的最简洁方法
- jQuery实现鼠标单击网页文字后在文本框显示的方法
- jQuery实现鼠标单击网页文字后在文本框显示的方法
- js实现键盘上下左右键选择文字并显示在文本框的方法
- js实现键盘上下左右键选择文字并显示在文本框的方法
- CSS实现同一行的图片和文字垂直居中对齐的方法
- CSS实现让同一行文字和输入框对齐的方法
- javascript实现点击后变换按钮显示文字的方法
- javascript实现点击后变换按钮显示文字的方法