标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 #import "KMLabel.h" 3 4 @interface ViewController () 5 - (void)layoutUI; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 self.title = @"设置屏幕中文本的横向对齐方式"; 23 24 CGRect newRect = CGRectMake(20.0, 40.0, 320.0, 160.0); 25 KMLabel *lblLeft = [[KMLabel alloc] initWithTextAlignment:NSTextAlignmentLeft]; 26 lblLeft.frame = newRect; 27 lblLeft.textColor = [UIColor blueColor]; 28 lblLeft.lineBreakMode = NSLineBreakByWordWrapping; 29 [self.view addSubview:lblLeft]; 30 31 KMLabel *lblCenter = [[KMLabel alloc] initWithTextAlignment:NSTextAlignmentCenter]; 32 newRect.origin.y += 170; 33 lblCenter.frame = newRect; 34 lblCenter.textColor = [UIColor brownColor]; 35 lblCenter.lineBreakMode = NSLineBreakByWordWrapping; 36 [self.view addSubview:lblCenter]; 37 38 KMLabel *lblRight = [[KMLabel alloc] initWithTextAlignment:NSTextAlignmentRight]; 39 newRect.origin.y += 170; 40 lblRight.frame = newRect; 41 lblRight.textColor = [UIColor darkGrayColor]; 42 lblRight.lineBreakMode = NSLineBreakByWordWrapping; 43 [self.view addSubview:lblRight]; 44 } 45 46 @end
KMLabel.h
1 #import <UIKit/UIKit.h> 2 3 @interface KMLabel : UILabel 4 - (id)initWithTextAlignment:(NSTextAlignment)textAlignment; 5 6 @end
KMLabel.m
1 #import "KMLabel.h" 2 3 @interface KMLabel () 4 @end 5 6 @implementation KMLabel 7 8 - (id)initWithTextAlignment:(NSTextAlignment)textAlignment { 9 if (self = [super init]) { 10 self.textAlignment = textAlignment; 11 self.backgroundColor = [UIColor whiteColor]; 12 self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | 13 UIViewAutoresizingFlexibleLeftMargin | 14 UIViewAutoresizingFlexibleBottomMargin | 15 UIViewAutoresizingFlexibleRightMargin; 16 self.layer.masksToBounds = YES; 17 self.layer.cornerRadius = 10.0; 18 self.layer.borderColor = [UIColor grayColor].CGColor; 19 self.layer.borderWidth = 1.0; 20 } 21 return self; 22 } 23 24 - (void)drawRect:(CGRect)rect { 25 NSString *strMsg; 26 switch (self.textAlignment) { 27 case NSTextAlignmentLeft: { 28 strMsg = @"左对齐\n左对齐\n左对齐"; 29 break; 30 } 31 case NSTextAlignmentCenter: { 32 strMsg = @"居中对齐\n居中对齐\n居中对齐"; 33 break; 34 } 35 case NSTextAlignmentRight: { 36 strMsg = @"右对齐\n右对齐\n右对齐"; 37 break; 38 } 39 default: { 40 break; 41 } 42 } 43 44 //进行绘制 45 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 46 // Set line break mode 47 paragraphStyle.lineBreakMode = self.lineBreakMode; 48 // Set text alignment 49 paragraphStyle.alignment = self.textAlignment; 50 paragraphStyle.lineSpacing = 5.0; //设置行间距 51 52 NSDictionary *dicAttribute = @{ 53 NSForegroundColorAttributeName : self.textColor, 54 NSFontAttributeName : [UIFont systemFontOfSize:18.0], 55 NSBaselineOffsetAttributeName : [NSNumber numberWithFloat:-10.0], //每行内容最顶基线开始垂直偏移量(会增加行间距),正浮点表示上方移动,负浮点表示下方移动 56 NSParagraphStyleAttributeName : paragraphStyle 57 }; 58 59 [strMsg drawInRect:rect withAttributes:dicAttribute]; 60 //效果跟上面语句差不多;但对宽度和垂直布局高度不能限制,而且横行对齐方式也不能限制,只能一行显示内容 61 //[strMsg drawAtPoint:rect.origin withAttributes:dicAttribute]; //The width (height for vertical layout) of the rendering area is unlimited, unlike drawInRect:withAttributes:, which uses a bounding rectangle. As a result, this method renders the text in a single line. 62 } 63 64 @end
141设置屏幕中文本的横向对齐方式(扩展知识:设置标签行间距)
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4581477.html