iOS控件篇之――UILabel


一,常规设置

    //设置字体:粗体,正常的是 SystemFontOfSize     

    label1.font = [UIFont boldSystemFontOfSize:20];     


    //设置文字颜色  

    label1.textColor = [UIColor orangeColor];     

    label2.textColor = [UIColor purpleColor];     


    //设置文字位置     

    label1.textAlignment = UITextAlignmentRight;     

    label2.textAlignment = UITextAlignmentCenter;     

    //设置字体大小适应label宽度     

    label4.adjustsFontSizeToFitWidth = YES;     

  

    //设置label的行数     


     label5.numberOfLines = 2;    

     UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色   

 

    //设置高亮     

    label6.highlighted = YES;     

    label6.highlightedTextColor = [UIColor orangeColor];     


    //设置阴影     

    label7.shadowColor = [UIColor redColor];     

    label7.shadowOffset = CGSizeMake(1.0,1.0);     

 

     //设置label中的文字是否可变,默认值是YES     

    label3.enabled = NO;  



  二,自动换行

                      

                    //高度自适应  

                    cellLabel.textAlignment = NSTextAlignmentLeft;

     cellLabel.lineBreakMode = UILineBreakModeWordWrap;

            cellLabel.numberOfLines = 0;

    

    //高度自适应

    CGRect txtFrame = cellLabel.frame;

    cellLabel.frame = CGRectMake(1510Main_Screen_Width-15*2,  txtFrame.size.height =[cellLabel.text boundingRectWithSize:CGSizeMake(txtFrame.size.widthCGFLOAT_MAXoptions:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:cellLabel.font,NSFontAttributeNamenilcontext:nil].size.height);        

          //自适应之后的大小                 cellLabel.frame

     

            三,下划线

       NSMutableAttributedString *content = [[NSMutableAttributedString allocinitWithString:@"This is a under line label"];

NSRange contentRange = {0, [content length]};

[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];


    

      四,调整行间距

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:myLabel.text];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:7];

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

    myLabel.attributedText = attributedString;

    [myLabel sizeToFit];



你可能感兴趣的:(ios,UILabel,控件,Object-C)