UILabel

目录

  1.1 UILabel被砍头
  1.2 设置行间距
  1.3 设置删除线
  1.4 宽固定,字缩小
  1.5 宽固定,高增加
  1.6 字体颜色渐变
  1.7 加边框
  1.8 字体过长显示形式
  1.9 跑马灯
  1.10 设置圆角
  1.11 首行缩进
  1.12 局部改变颜色
  1.13 自适应标签云
  1.14 遍历字体库
1.1 UILabel被砍头

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 14)];

label.text = @"设置高度为14,看实际打印高度";

CGSize size = [label.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];

NSLog(@"高度:%f",size.height);

高度:16.707031


1.2 设置行间距

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];//调整行间距

[attributedString addAttribute:NSParagraphStyleAttributeName 
                         value:paragraphStyle 
                         range:NSMakeRange(0, [labelText length])];

label.attributedText = attributedString;

[self.view addSubview:label];

[label sizeToFit];

1.3 设置删除线

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;剪切与文本宽度相同的内容长度,后半部分被删除。

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 设置圆角

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 首行缩进

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

// 首行缩进
style.firstLineHeadIndent = 50.0f;

// 头部缩进 
//style.headIndent = 100;

// 尾部缩进 
//style.tailIndent = -10.0f;

NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"演示首行缩进" 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];

//设置:在0-3个单位长度内的内容显示成红色

[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 自适应标签云

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    CGFloat screen_Width = [UIScreen mainScreen].bounds.size.width;
    
    NSArray *dataArray = @[@"从什么都没有的地方",@"到什么都没有的地方",@"我们像没发生事一样",@"自顾地",@"走在路上",@"忘掉了的人只是泡沫",@"用双手轻轻一触就破",@"泛黄",@"有他泛黄的理由",@"思念将",@"越来越薄"];
    
    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.设置字体字号
    
    label.font = [UIFont fontWithName:@"Georgia" size:20];

你可能感兴趣的:(UILabel)