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

你可能感兴趣的:(xcode)