首页 > 移动平台 > 详细

iOS常用技术-竖立的Label

时间:2016-01-20 22:21:12      阅读:229      评论:0      收藏:0      [点我收藏+]

//
//  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
/*********************************************************************/

技术分享

iOS常用技术-竖立的Label

原文:http://www.cnblogs.com/MrWuYindi/p/5146622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!