标签:
UIlabel
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10,30, CGRectGetWidth(self.view.frame) -20, 200)];
//设置文字
label.text = @"欢迎收看灌篮高手,我是安溪教练";
//设置文字颜色
label.textColor = [UIColor grayColor];
//默认17号字体
label.font = [UIFont systemFontOfSize:34];
//对齐方式
label.textAlignment = NSTextAlignmentCenter;
//设置阴影
label.shadowColor = [UIColor redColor];
//设置阴影映射大小,默认CGSizeMake(0, -1)
label.shadowOffset = CGSizeMake(-2, -2);
//断句,展示缩略
label.lineBreakMode = NSLineBreakByTruncatingTail;
//行数0为自动匹配行数
label.numberOfLines = 1;
//字体是否适应宽度(Size无效)
label.adjustsFontSizeToFitWidth = YES;
//最小字体比例
// label.minimumScaleFactor = 0.9;
UIfont
输出编译器所有的字体
for (NSString * familyName in [UIFont familyNames]) {
NSLog(@"%@",familyName);
NSArray * familys = [UIFont fontNamesForFamilyName:familyName];
for (NSString * fontName in familys) {
NSLog(@"%@",fontName);
}
}
具体实现方法
//斜体
//[UIFont italicSystemFontOfSize:50];
//字体加粗
//[UIFont boldSystemFontOfSize:50];
//设置系统字体
//[UIFont systemFontOfSize:10];
//[UIFont fontWithName:@"Heiti TC" size:25];
富文本
段落设置
NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10;//行间距
paragraph.paragraphSpacing = 50;//段间距
paragraph.firstLineHeadIndent = 50;//第一段空几格
富文本实现
NSDictionary * dictA = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:[UIColor greenColor],//前景色
NSBackgroundColorAttributeName:[UIColor grayColor],//后景色
NSParagraphStyleAttributeName:paragraph
// NSObliquenessAttributeName:@0.5 //斜体
// NSStrokeColorAttributeName:[UIColor whiteColor],
// NSStrokeWidthAttributeName:@2,//描边
// NSKernAttributeName:@20,//字间距
// NSStrikethroughStyleAttributeName:@2,//删除线
// NSUnderlineStyleAttributeName:@1,//下划线
};
NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:exampleString attributes:dictA];
可变富文本
NSMutableAttributedString * astring = [[NSMutableAttributedString alloc] initWithString:string];
//可变富文本
NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc] initWithString:exmapleString];
NSRange range1 = {0,3};
NSRange range2 = {4,5};
[mutableAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:range1];
NSDictionary * dict = @{NSForegroundColorAttributeName:[UIColor magentaColor],
NSBackgroundColorAttributeName:[UIColor cyanColor]};
[mutableAttribute addAttributes:dict range:range2];
追加子符串
NSAttributedString * string2 = [[NSAttributedString alloc] initWithString:@"好哒!" attributes:dict2];
[astring appendAttributedString:string2];
计算label的高度
//一定要先确定宽度,再根据宽度和字体计算size—多行显示
CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
单行显示
NSString * string = @"黑猫警长";
NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:50]};
CGSize size = [string sizeWithAttributes:dict];
label的自适应-多行
UILabel * label = [[UILabel alloc] init];
label.text = string;//先给字符串,再计算它的size
label.numberOfLines = 0;
label.backgroundColor = [UIColor greenColor];
CGSize size = [label sizeThatFits:CGSizeMake(self.view.frame.size.width - 40, MAXFLOAT)];
label.frame = CGRectMake(20, 50, size.width, size.height);
[self.view addSubview:label];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 0)];
label.text = string;
label.numberOfLines = 0;
label.backgroundColor = [UIColor greenColor];
//使用sizeToFit一定要设置宽度
[label sizeToFit];
[self.view addSubview:label];
标签:
原文地址:http://www.cnblogs.com/iOSlearner/p/5159327.html