目錄
1.1 UILabel被砍頭
1.2 設(shè)置行間距
1.3 設(shè)置刪除線
1.4 寬固定,字縮小
1.5 寬固定鹅很,高增加
1.6 字體顏色漸變
1.7 加邊框
1.8 字體過長顯示形式
1.9 跑馬燈
1.10 設(shè)置圓角
1.11 首行縮進(jìn)
1.12 局部改變顏色
1.13 自適應(yīng)標(biāo)簽云
1.14 遍歷字體庫
1.1 UILabel被砍頭
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 14)];
label.text = @"設(shè)置高度為14嘶居,看實(shí)際打印高度";
CGSize size = [label.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
NSLog(@"高度:%f",size.height);
高度:16.707031
1.2 設(shè)置行間距
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(0, 100, 320, 200)];
[label setBackgroundColor:[UIColor blackColor]];
[label setTextColor:[UIColor whiteColor]];
[label setNumberOfLines:0];
NSString *labelText = @"可以自己按照寬高,字體大小,來計算有多少行邮屁。";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle =
[[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:5];//調(diào)整行間距
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[self.view addSubview:label];
[label sizeToFit];
1.3 設(shè)置刪除線
UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 20)];
NSString *priceString =@"$88.88";
NSAttributedString *attrStr = [[NSAttributedString alloc]init
WithString:priceString attributes: @{
NSFontAttributeName:[UIFont systemFontOfSize:20.f],
NSForegroundColorAttributeName:[UIColor orangeColor],
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|
NSUnderlinePatternSolid),
NSStrikethroughColorAttributeName:[UIColor blackColor]
}];
priceLabel.attributedText = attrStr;
[self.view addSubview:priceLabel];
1.4 寬固定整袁,字縮小
priceLabel.adjustsLetterSpacingToFitWidth = NO;
1.5 寬固定,高增加
UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)];
textLabel.backgroundColor = [UIColor lightTextColor];
[textLabel setNumberOfLines:0];
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.font = [UIFont fontWithName:@"Arial" size:15];
CGSize size = CGSizeMake(100, 1000);
textLabel.text = @"UILable是iPhone界面最基本的控件佑吝,主要用來顯示文本信息坐昙。";
CGSize msgSie = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:size];
[textLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];
[self.view addSubview:textLabel];
NSLog(@"%f",textLabel.frame.size.height);
1.6 字體顏色漸變
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
1.7 加邊框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;
1.8 字體過長顯示形式
例如:UILable是iPhone界面最基本的控件,主要用來顯示文本信息芋忿。
label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯示炸客,后面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內(nèi)容長度戈钢,后半部分被刪除痹仙。
label.lineBreakMode = NSLineBreakByTruncatingHead; 例如:...信息。
label.lineBreakMode = NSLineBreakByTruncatingMiddle; 例如:UILable...信息殉了。
label.lineBreakMode = NSLineBreakByTruncatingTail; 例如:UILable是iPhone界面最基本的控件...
label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯示开仰,后面部分省略不顯示。
1.9 跑馬燈
UILabel *runLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,200, 300, 100)];
runLabel.text =@"嚕啦啦嚕啦啦嚕啦嚕啦嚕薪铜,嚕啦嚕啦嚕啦嚕啦嚕啦嚕~~~";
[self.view addSubview:runLabel];
CGRect frame = runLabel.frame;
frame.origin.x = -180;
runLabel.frame = frame;
[UIView beginAnimations:@"testAnimation"context:NULL];
[UIView setAnimationDuration:5.8f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:999999];
frame = runLabel.frame;
frame.origin.x =350;
runLabel.frame = frame;
[UIView commitAnimations];
1.10 設(shè)置圓角
UILabel *cornerLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
cornerLabel.layer.cornerRadius = 50;
cornerLabel.clipsToBounds = YES;
cornerLabel.text = @"檸檬";
cornerLabel.textAlignment = NSTextAlignmentCenter;
cornerLabel.backgroundColor =[UIColor redColor];
cornerLabel.textColor = [UIColor whiteColor];
[self.view addSubview:cornerLabel];
1.11 首行縮進(jìn)
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
// 首行縮進(jìn)
style.firstLineHeadIndent = 50.0f;
// 頭部縮進(jìn)
//style.headIndent = 100;
// 尾部縮進(jìn)
//style.tailIndent = -10.0f;
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"演示首行縮進(jìn)" attributes:@{ NSParagraphStyleAttributeName : style}];
UILabel *IndentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; IndentLabel.font = [UIFont systemFontOfSize:17];
IndentLabel.backgroundColor = [UIColor redColor];
IndentLabel.attributedText = attrText;
[self.view addSubview:IndentLabel];
1.12 局部改變顏色
示例:我本月提出反饋 22 個!
NSString *bugCount = @"我本月提出反饋 22 個!";
NSMutableAttributedString *bugCountstr = [[NSMutableAttributedString alloc]initWithString:bugCount];
//設(shè)置:在0-3個單位長度內(nèi)的內(nèi)容顯示成紅色
[bugCountstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 2)];
UILabel *bugLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 20)];
bugLabel.attributedText =bugCountstr;
[self.view addSubview:bugLabel];
1.13 自適應(yīng)標(biāo)簽云
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat screen_Width = [UIScreen mainScreen].bounds.size.width;
NSArray *dataArray = @[@"從什么都沒有的地方",@"到什么都沒有的地方",@"我們像沒發(fā)生事一樣",@"自顧地",@"走在路上",@"忘掉了的人只是泡沫",@"用雙手輕輕一觸就破",@"泛黃",@"有他泛黃的理由",@"思念將",@"越來越薄"];
int k = 0;
for (int i=0; i < dataArray.count; i++)
{
CGSize size=[[dataArray objectAtIndex:i] boundingRectWithSize:CGSizeMake(150, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
double w = size.width;
double h = 30;
double x = 10;
double y = 64;
if (i==0)
{
x = 20;
y = 64;
}else{
UILabel *titleLabel = (UILabel *)[self.view viewWithTag:100+i-1];
if (titleLabel.frame.origin.x+titleLabel.frame.size.width+10+w <= screen_Width)
{
x = titleLabel.frame.origin.x+titleLabel.frame.size.width+20;
y = 64+(h+10)*k;
}
else
{
k++;
x = 20;
y = 64+(h+10)*k;
}
}
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
titleLabel.font = [UIFont systemFontOfSize:12];
titleLabel.text = dataArray[i];
titleLabel.numberOfLines = 0;
titleLabel.userInteractionEnabled = YES;
titleLabel.textColor = [UIColor purpleColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchClick:)];
[titleLabel addGestureRecognizer:tap];
titleLabel.tag = 100+i;
[self.view addSubview:titleLabel];
}
}
-(void)searchClick:(UITapGestureRecognizer *)tap
{
NSLog(@"%@",((UILabel *)tap.view).text);
}
1.14 遍歷字體庫
NSArray *names = [UIFont familyNames];
for(NSString *name in names)
{
NSLog(@"%@",name);
}
// 2.設(shè)置字體字號
label.font = [UIFont fontWithName:@"Georgia" size:20];