1. 程式人生 > >IOS中UILabel控制元件的基本使用

IOS中UILabel控制元件的基本使用

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