码迷,mamicode.com
首页 > 其他好文 > 详细

UILabel属性小解

时间:2016-05-28 12:49:41      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    NSString * context = @"\n27日下午,美国总统奥巴马首度访问二战核爆地——日本广岛,\n在和平纪念公园向核爆纪念碑献花圈。任期接近尾声的奥巴马,\n为了无核武的理想,最终踏上被美国核弹轰炸过的广岛。\n\n在他来之前的70多年里,美国的历任总统,没有任何一位在\n任期内来过。作为美国总统去直面核爆受害者,奥巴马的广岛之行\n从一开始就备受热议,外界更关注奥巴马是否会道歉,日本右翼人士则直接\n要求他向成千上万核爆受害者道歉。对此,奥巴马临行前接受日\n媒专访时曾明确表示,不会道歉,\n“检讨当年投放原子弹的对错,那是历史学家的工作”。";

 

    //字体大小

    UIFont *font = [UIFont systemFontOfSize:18];

    //行间距

    CGFloat lineSpace = 8;

    //段间距

    CGFloat paragraphSpacing = 0;

    //label宽高

    CGSize labSize = CGSizeMake(400, 1000);

    //字间距

    NSNumber *textLengthSpace  = @0.2;

 

    NSDictionary *dic = [self setTextLineSpaceWithString:context  withFont:font withLineSpace:lineSpace withTextlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

 

    CGSize size = [context boundingRectWithSize:labSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    CGFloat sizeHeight = size.height;

 

    //CGFloat sizeHeight =  [self getSpaceLabelHeight:conText withFont:font withLineSpace:lineSpace size:labSize textlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

 

    UILabel  *label = [[UILabel alloc] init];

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor purpleColor];

    [label sizeToFit];

    label.frame =  CGRectMake(40, 40, 300, sizeHeight);

    label.attributedText = [[NSAttributedString alloc] initWithString:context attributes:dic];

    [self.view addSubview:label];

 

}

/*

 *给UILabel设置行间距和字间距

 */

-(NSDictionary *)setTextLineSpaceWithString:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = lineSpace; //设置行间距

    paraStyle.hyphenationFactor = 1.0;

    paraStyle.firstLineHeadIndent = 0.0;

    paraStyle.paragraphSpacingBefore = 0.0;

    paraStyle.headIndent = 0;

    paraStyle.tailIndent = 0;

    NSDictionary *dic = @{NSFontAttributeName:font,

                          NSParagraphStyleAttributeName:paraStyle,

                          NSKernAttributeName:textlengthSpace

                        };

    return dic;

    

}

/*

 *计算UILabel的高度(带有行间距的情况)

 */

//-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace size:(CGSize)textSize textlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing

//{

//    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

//    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

//    paraStyle.alignment = NSTextAlignmentLeft;

//    paraStyle.lineSpacing = lineSpace;

//    paraStyle.paragraphSpacing = paragraphSpacing;

//    paraStyle.hyphenationFactor = 1.0;

//    paraStyle.firstLineHeadIndent = 0.0;

//    paraStyle.paragraphSpacingBefore = 0.0;

//    paraStyle.headIndent = 0;

//    paraStyle.tailIndent = 0;

 

//    NSDictionary *dic = @{NSFontAttributeName:font,

//                          NSParagraphStyleAttributeName:paraStyle,

//                          NSKernAttributeName:textlengthSpace

//                          };

 

//    CGSize size = [str boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

 

//    return size.height;

//}

@end

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    NSString * context = @"\n27日下午,美国总统奥巴马首度访问二战核爆地——日本广岛,\n在和平纪念公园向核爆纪念碑献花圈。任期接近尾声的奥巴马,\n为了无核武的理想,最终踏上被美国核弹轰炸过的广岛。\n\n在他来之前的70多年里,美国的历任总统,没有任何一位在\n任期内来过。作为美国总统去直面核爆受害者,奥巴马的广岛之行\n从一开始就备受热议,外界更关注奥巴马是否会道歉,日本右翼人士则直接\n要求他向成千上万核爆受害者道歉。对此,奥巴马临行前接受日\n媒专访时曾明确表示,不会道歉,\n“检讨当年投放原子弹的对错,那是历史学家的工作”。";

 

    //字体大小

    UIFont *font = [UIFont systemFontOfSize:18];

    //行间距

    CGFloat lineSpace = 8;

    //段间距

    CGFloat paragraphSpacing = 0;

    //label宽高

    CGSize labSize = CGSizeMake(400, 1000);

    //字间距

    NSNumber *textLengthSpace  = @0.2;

 

    NSDictionary *dic = [self setTextLineSpaceWithString:context  withFont:font withLineSpace:lineSpace withTextlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

 

    CGSize size = [context boundingRectWithSize:labSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    CGFloat sizeHeight = size.height;

 

    //CGFloat sizeHeight =  [self getSpaceLabelHeight:conText withFont:font withLineSpace:lineSpace size:labSize textlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

 

    UILabel  *label = [[UILabel alloc] init];

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor purpleColor];

    [label sizeToFit];

    label.frame =  CGRectMake(40, 40, 300, sizeHeight);

    label.attributedText = [[NSAttributedString alloc] initWithString:context attributes:dic];

    [self.view addSubview:label];

 

}

/*

 *给UILabel设置行间距和字间距

 */

-(NSDictionary *)setTextLineSpaceWithString:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = lineSpace; //设置行间距

    paraStyle.hyphenationFactor = 1.0;

    paraStyle.firstLineHeadIndent = 0.0;

    paraStyle.paragraphSpacingBefore = 0.0;

    paraStyle.headIndent = 0;

    paraStyle.tailIndent = 0;

    NSDictionary *dic = @{NSFontAttributeName:font,

                          NSParagraphStyleAttributeName:paraStyle,

                          NSKernAttributeName:textlengthSpace

                        };

    return dic;

    

}

/*

 *计算UILabel的高度(带有行间距的情况)

 */

//-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace size:(CGSize)textSize textlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing

//{

//    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

//    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

//    paraStyle.alignment = NSTextAlignmentLeft;

//    paraStyle.lineSpacing = lineSpace;

//    paraStyle.paragraphSpacing = paragraphSpacing;

//    paraStyle.hyphenationFactor = 1.0;

//    paraStyle.firstLineHeadIndent = 0.0;

//    paraStyle.paragraphSpacingBefore = 0.0;

//    paraStyle.headIndent = 0;

//    paraStyle.tailIndent = 0;

 

//    NSDictionary *dic = @{NSFontAttributeName:font,

//                          NSParagraphStyleAttributeName:paraStyle,

//                          NSKernAttributeName:textlengthSpace

//                          };

 

//    CGSize size = [str boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

 

//    return size.height;

//}

@end

附上效果图

 技术分享

UILabel属性小解

标签:

原文地址:http://www.cnblogs.com/liyuqaing0011/p/5537014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!