UILabel属性小解

#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

附上效果图

时间: 2024-08-09 21:50:54

UILabel属性小解的相关文章

IOS UILabel属性

1  textAlignment有三种设置方式:(以UI开头,是ios6之前的用法) (1)  NSTextAlignmentLeft        为向左对齐 (2)  NSTextAlignmentCenter    为居中对齐 (3)  NSTextAlignmentRight      为向右对齐 2  linBreakMode(可选值) (当文本内容很多,label无法全部显示时label会将文本内容以省略号的方式代替) enum{ NSLineBreakByWordWrapping 

IOS开发UI基础UILabel属性

UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first";  NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text];  [textLabelStr setAttributes:@{NSForegroundColorAttribute

UILabel属性解析

UILabel 这是是用来显示文字最常用的一个控件, ?用户不可以输入,不可以点击 ? Text ?输入的文字以及显示的字体 Color ? ?字体颜色 Font ? ? 字体类型 默认有3个样式可以选择 1.systemFontOfSize ? ? 系统样式 2.boldSystemFontOfSize ? ?粗体样式 3.italicSystemFontOfSize ? 斜体样式 ? Alignment ? ?对齐方式 Lines ? ? ? 设置文字以多少行显示, ?值为0时候表示自动换行

UILabel属性

1 UILabel控件属性: 2 1.text:设置标签显示文本. 3 4 2.attributedText:设置标签属性文本. 5 6 Ios代码 7 8 NSString *text = @"first"; 9 10 NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text]; 11 12 [textLabelStr setAttributes:@

UIlabel 属性text

UILabel *pLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,200,100)]; pLabel.text = @"测试到吗"; NSInteger leght = [pLabel.text length]; NSMutableAttributedString *richText = [[NSMutableAttributedString alloc] initWithString:pLabel.text]; //设

CSS float属性小解——”浮“生若水

学习CSS一段时间了,抽时间总结一下,算是对内容的一个回顾和分享.如果有什么讲的不好的,但求轻喷~~~ 一.float是什么 回答float是什么这个问题,最好的答案莫过于官方回答,故而在此先引用一段W3C官方文档: Floats. In the float model, a box is first laid out according to the normal flow, then taken out of the flow and shifted to the left or right

文档学习 - UILabel - 属性详解

#import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1. UILabel *label = [[UILabel alloc] init]; // 1.1 text label.text = @"//  ViewController.m01-UILabelCreated byjiaguanglei on 15/9/30Cop

【IOS】UILabel 属性及方法

label.lineBreakMode = NSLineBreakByTruncatingTail; //文尾省略号 label.font = [UIFont fontWithName:@"AvenirNext-Bold" size:12]; //字体 label.numberOfLines = 1; //行数 label.textAlignment = NSTextAlignmentLeft; /NSTextAlignmentCenter //字体位置 //混合样式的文字 UICol

简述UILabel的属性和用法

UILabel属性 1.text:设置标签显示文本. label.text = @"我是Label"; 2.attributedText:设置标签属性文本. NSString *text = @"first"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text]; [textLabelStr setAttributes:@{