//
// VerticalLabel.h
// VerticalLabel
//
// Created by 大欢 on 16/1/19.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface VerticalLabel : UILabel
- (instancetype)initWithVerticalText:(NSString *)string fontSize:(CGFloat)width;
@end
/******************************************************************/
//
// VerticalLabel.m
// VerticalLabel
//
// Created by 大欢 on 16/1/19.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "VerticalLabel.h"
#define lineWidth 50
@implementation VerticalLabel
- (instancetype)initWithVerticalText:(NSString *)string fontSize:(CGFloat)width {
if (self = [super init]) {
NSMutableParagraphStyle * paragarph = [[NSMutableParagraphStyle alloc] init];
paragarph.lineSpacing = lineWidth;
NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:width],
NSParagraphStyleAttributeName:paragarph};
NSAttributedString * astring = [[NSAttributedString alloc] initWithString:string attributes:dict];
//MAXFLOAT
CGSize size = [string boundingRectWithSize:CGSizeMake(width, 480) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
self.text = string;
self.attributedText = astring;
self.lineBreakMode = NSLineBreakByCharWrapping;
self.numberOfLines = 0;
self.bounds = CGRectMake(0, 0, size.width, size.height);
}
return self;
}
@end
/*****************************************************************************/
//
// ViewController.m
// VerticalLabel
//
// Created by 大欢 on 16/1/19.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "ViewController.h"
#import "VerticalLabel.h"
@interface ViewController ()
@property (nonatomic, strong) VerticalLabel * label;
@end
@implementation ViewController
- (VerticalLabel *)label {
if (!_label) {
_label = [[VerticalLabel alloc] initWithVerticalText:@"哈哈哈哈" fontSize:50];
_label.backgroundColor = [UIColor greenColor];
_label.center = self.view.center;
}
return _label;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
/*********************************************************************/
原文:http://www.cnblogs.com/MrWuYindi/p/5146622.html