UILable

//UILable的大小自適應實例  
    UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];//設定位置與大小  
    [myLable setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];//格式  
    [myLable setNumberOfLines:0];//行數,只有設爲0才能自適應  
    [myLable setBackgroundColor:[UIColor clearColor]];//背景色  
    myLable.shadowColor = [UIColor darkGrayColor];//陰影顏色  
    myLable.shadowOffset = CGSizeMake(1., 1.0);//陰影大小  
      
    NSString *text = @"abcdefghijklmnopqrstuvwxyz";  
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];  
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];  
    CGRect rect = myLable.frame;  
    rect.size = size;  
    [myLable setFrame:rect];  
    [myLable setText:text];  
    myLable.shadowColor = [UIColor darkGrayColor];//陰影顏色  
    myLable.shadowOffset =  CGSizeMake(2.0, 2.0);//陰影大小  
    [self.view addSubview:myLable];  
    [myLable release];  
      
    //UILable的基本用法  
    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 40.0, 200.0, 30.0)];  
    UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];  
    UILabel *lbl3 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];  
    UILabel *lbl4 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];  
    UILabel *lbl5 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];  
    UILabel *lbl6 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];  
    UILabel *lbl7 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];  
      
    //設置顯示文字  
    lbl1.text = @"lable1";  
    lbl2.text = @"lable2";  
    lbl3.text = @"lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--11個";  
    lbl4.text = @"lable4--lable4--lable4--lable4--4個";  
    lbl5.text = @"lable5--lable5--lable5--lable5--lable5--lable5--6個";  
    lbl6.text = @"lable6";  
    lbl7.text = @"lable7";  
      
    //設置字體:粗體,正常的是SystemFontOfSize  
    lbl1.font = [UIFont boldSystemFontOfSize:20];  
    //設置文字顏色  
    lbl1.textColor = [UIColor orangeColor];  
    lbl2.textColor = [UIColor purpleColor];  
    //設置背景顏色  
    lbl1.backgroundColor = [UIColor clearColor];  
    lbl2.backgroundColor = [UIColor colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];  
    //設置字體位置  
    lbl1.textAlignment = UITextAlignmentRight;  
    lbl2.textAlignment = UITextAlignmentCenter;  
    //設置字體的小適應lable的寬度  
    lbl4.adjustsFontSizeToFitWidth = YES;  
    //設置lable 的行數  
    lbl5.numberOfLines = 2;  
      
    //設置高亮  
    lbl6.highlighted = YES;  
    lbl6.highlightedTextColor = [UIColor orangeColor];  
    //設置陰影  
    lbl7.shadowColor = [UIColor redColor];  
    lbl7.shadowOffset = CGSizeMake(1.0, 1.0);  
      
    //設置是否能與用戶進行交互  
    lbl7.userInteractionEnabled = YES;  
    //設置lable中文字是否可變,默認爲YES;  
    lbl3.enabled = NO;  
    //設置lable中文字過長時的顯示格式  
    lbl3.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中間  
//    typedef enum{  
//        UILineBreakModeWordWrap = 0,  
//        UILineBreakModeCharacterWrap,  
//        UILineBreakModeClip,//截去多餘部分  
//        UILineBreakModeHeadTruncation,//截取頭部  
//        UILineBreakModeTailTruncation,//截去尾部  
//        UILineBreakModeMiddleTruncation,//截去中間  
//    }UILineBreakMode;  
      
    //若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就用來控制文本基線的行爲  
    lbl4.baselineAdjustment = UIBaselineAdjustmentNone;  
    [self.view addSubview:lbl1];  
    [self.view addSubview:lbl2];  
    [self.view addSubview:lbl3];  
    [self.view addSubview:lbl4];  
    [self.view addSubview:lbl5];  
    [self.view addSubview:lbl6];  
    [self.view addSubview:lbl7];  
      
    [lbl1 release];  
    [lbl2 release];  
    [lbl3 release];  
    [lbl4 release];  
    [lbl5 release];  
    [lbl6 release];  
    [lbl7 release];  
相關文章
相關標籤/搜索