標籤:
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];
ios學習day3