3种方法实现UILabel的左上角对齐显示文字
来源:互联网 发布:淘宝推广pid 编辑:程序博客网 时间:2024/04/29 23:30
1、继承UILabel 改变origin的值重新绘制
@interface TopLeftLabel :UILabel
@end
#import"TopLeftLabel.h"
@implementation TopLeftLabel
- (id)initWithFrame:(CGRect)frame {
return [superinitWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
// 重新绘制text的位置
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [selftextRectForBounds:requestedRectlimitedToNumberOfLines:self.numberOfLines];
[superdrawTextInRect:actualRect];
}
@end
2、第二种方法 写一个UILabel的分类
@interface UILabel (LeftTopAlign)
- (void) textLeftTopAlign;
@end
@implementation UILabel (LeftTopAlign)
- (void) textLeftTopAlign
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init];
paragraphStyle.lineBreakMode =NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:17],NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [self.textboundingRectWithSize:CGSizeMake(207,999) options:NSStringDrawingUsesLineFragmentOriginattributes:attributes context:nil].size;
CGRect dateFrame =CGRectMake(2,140, CGRectGetWidth(self.frame)-5, labelSize.height);
self.frame = dateFrame;
}
@end
3、直接用UITextView代替 UILabel 然后禁用UITextView 的滚动效果
- 3种方法实现UILabel的左上角对齐显示文字
- iOS UILabel 文字自动左上角对齐
- UILabel文字上对齐方法
- UILabel 的文字顶部对齐
- UILabel的文字顶部对齐
- 纯UILabel实现文字的竖排显示
- QMessageBox 显示文字对齐的实现
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- 让UILabel的文字顶部对齐
- iOS -让UILabel的文字顶部对齐
- label实现左上角对齐
- CSS控制图片和文字在同一行显示且对齐的3种方法
- 让UILabel 显示文字斜体加粗的简单方法
- OPENCV入门教程二十一:图像金字塔上下采样
- 新产品
- POJ 2762 Going from u to v or from v to u(弱连通分量)
- 立体页面切换
- 陆维家具设计拆单软件
- 3种方法实现UILabel的左上角对齐显示文字
- [HDU1789]-Doing Homework again
- Android怎样播放多张图片形成一个动画
- 对LinkedHashMap的理解
- git安装与使用提交到github
- 泛型超详细解读(二):super和extend
- UVA10300 - Ecological Premium
- httpclient4下载图片 java实现
- #411 Gray Code