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];



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章