iOS常用技术-竖立的Label

 1 //
 2 //  VerticalLabel.h
 3 //  VerticalLabel
 4 //
 5 //  Created by 大欢 on 16/1/19.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface VerticalLabel : UILabel
12 
13 - (instancetype)initWithVerticalText:(NSString *)string fontSize:(CGFloat)width;
14 
15 @end

/******************************************************************/

 1 //
 2 //  VerticalLabel.m
 3 //  VerticalLabel
 4 //
 5 //  Created by 大欢 on 16/1/19.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "VerticalLabel.h"
10 
11 #define lineWidth 50
12 
13 @implementation VerticalLabel
14 
15 - (instancetype)initWithVerticalText:(NSString *)string fontSize:(CGFloat)width {
16     
17     if (self = [super init]) {
18         
19         NSMutableParagraphStyle * paragarph = [[NSMutableParagraphStyle alloc] init];
20         paragarph.lineSpacing = lineWidth;
21         
22         NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:width],
23                                 NSParagraphStyleAttributeName:paragarph};
24         
25         NSAttributedString * astring = [[NSAttributedString alloc] initWithString:string attributes:dict];
26                                                                 //MAXFLOAT
27         CGSize size = [string boundingRectWithSize:CGSizeMake(width, 480) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
28         
29         self.text = string;
30         self.attributedText = astring;
31         self.lineBreakMode = NSLineBreakByCharWrapping;
32         self.numberOfLines = 0;
33         self.bounds = CGRectMake(0, 0, size.width, size.height);
34     }
35     return self;
36 }
37 @end

/*****************************************************************************/

 1 /
 2 //  ViewController.m
 3 //  VerticalLabel
 4 //
 5 //  Created by 大欢 on 16/1/19.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "VerticalLabel.h"
11 
12 @interface ViewController ()
13 @property (nonatomic, strong) VerticalLabel * label;
14 @end
15 
16 @implementation ViewController
17 
18 - (VerticalLabel *)label {
19     
20     if (!_label) {
21         _label = [[VerticalLabel alloc] initWithVerticalText:@"哈哈哈哈" fontSize:50];
22         _label.backgroundColor = [UIColor greenColor];
23         _label.center = self.view.center;
24     }
25     return _label;
26 }
27 
28 - (void)viewDidLoad {
29     [super viewDidLoad];
30     // Do any additional setup after loading the view, typically from a nib.
31     [self.view addSubview:self.label];
32 }
33 
34 - (void)didReceiveMemoryWarning {
35     [super didReceiveMemoryWarning];
36     // Dispose of any resources that can be recreated.
37 }
38 
39 @end


/*********************************************************************/

原文地址:https://www.cnblogs.com/MrWuYindi/p/5146622.html