2011-09-15 38 views
12

tôi đã làm theo mã sau:tạo lập trình một UILabel

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 300, 50)]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = UITextAlignmentCenter; // UITextAlignmentCenter, UITextAlignmentLeft 
    label.textColor=[UIColor whiteColor]; 
    label.text = @"Telechargez et consultez les catalogues et les tarifs de la gamme Audi au format PDF"; 
    [self.view addSubview:label]; 

Và có vẻ như this nhưng tôi muốn nó trông giống như this. Làm thế nào để thay đổi các thuộc tính của nhãn?

Trả lời

7

Để hiển thị UILable như của bạn được hiển thị trong hình ảnh của bạn, bạn cần phải thiết lập thuộc tính của UILabel sau và cũng có thể tăng chiều cao của Label của bạn.

@property(nonatomic) NSInteger numberOfLines; 
@property(nonatomic) UILineBreakMode lineBreakMode; 

nên được như như dưới đây ..

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 300, 100)]; 
    ................................. 
    label.numberOfLines=0; 
    label.lineBreakMode=UILineBreakModeCharacterWrap; 
    ............................ 
+2

Tính đến tháng chín-05-2013 UILineBreakModeCharacterWrap Deprecated của nó. Thay vào đó, bạn có thể sử dụng label.lineBreakMode = NSLineBreakByCharWrapping sẽ thực hiện tương tự như cách khác. – Jiraheta

+0

Cảm ơn, bạn có thể chỉnh sửa câu trả lời của tôi. – Jhaliya

1

Đặt numberOfLines thuộc tính của UILabel.

label.lineBreakMode = UILineBreakModeWordWrap; 
    label.numberOfLines = 3; 
    label.text = @"Telechargez et consultez les catalogues et les tarifs de la gamme Audi au format PDF"; 
2

nếu bạn biết số lượng các dòng tức là nếu số dòng là 3 thì bạn có thể viết

label.numberOfLines=3; 
label.lineBreakMode=UILineBreakModeCharacterWrap; 

và nếu u không biết chính xác dòng cho nhãn sau đó bạn có thể viết

label.numberOfLines=0; 
label.lineBreakMode=UILineBreakModeCharacterWrap; 
1

Đặt Numberoflines thuộc tính nhãn của bạn và sau đó cũng tăng độ rộng của lable để nhãn của bạn có thể hiển thị chính xác.

Thuộc tính này kiểm soát số lượng dòng tối đa cần sử dụng để vừa với văn bản của nhãn vào hình chữ nhật bao quanh của nhãn. Giá trị mặc định cho thuộc tính này là 1. Để xóa mọi giới hạn tối đa và sử dụng nhiều dòng nếu cần, đặt giá trị của thuộc tính này thành 0.

Nếu bạn hạn chế văn bản bằng thuộc tính này, bất kỳ văn bản nào không phù hợp với số lượng đường tối đa và bên trong hình chữ nhật giới hạn của nhãn được cắt ngắn bằng chế độ ngắt dòng thích hợp.

read more

17

Hãy thử điều này:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.textColor = [UIColor whiteColor]; 
    label.numberOfLines = 0; 
    label.lineBreakMode = UILineBreakModeWordWrap; 
    label.text = @"Telechargez et consultez les catalogues et les tarifs de la gamme Audi au format PDF"; 
    [self.view addSubview:label]; 
+0

Guys sử dụng NSTextAlignmentCenter cho UITextAlignmentCenter và vân vân cảnh báo deprecation – khunshan

2

Một Tiểu Thay đổi trên iOS 6 trở lên là

label.textAlignment = UITextAlignmentCenter; 

bị phản đối nên sử dụng

label.textAlignment = NSTextAlignmentLeft; 

thay thế.

0

đây là cách tạo UILabel Lập trình ..

1) Viết tệp này vào tệp .h của dự án.

UILabel *label; 

2) Viết tệp .m này vào dự án của bạn.

label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];//Set frame of label in your viewcontroller. 
[label setBackgroundColor:[UIColor lightGrayColor]];//Set background color of label. 
[label setText:@"Label"];//Set text in label. 
[label setTextColor:[UIColor blackColor]];//Set text color in label. 
[label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label. 
[label setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];//Set line adjustment. 
[label setLineBreakMode:NSLineBreakByCharWrapping];//Set linebreaking mode.. 
[label setNumberOfLines:1];//Set number of lines in label. 
[label.layer setCornerRadius:25.0];//Set corner radius of label to change the shape. 
[label.layer setBorderWidth:2.0f];//Set border width of label. 
[label setClipsToBounds:YES];//Set its to YES for Corner radius to work. 
[label.layer setBorderColor:[UIColor blackColor].CGColor];//Set Border color. 
[self.view addSubview:label];//Add it to the view of your choice. 
1

Trong Swift sử dụng này,

var label:UILabel = UILabel(frame: CGRectMake(0, 0, 70, 20)) 
    label.center = CGPointMake(50, 70) 
    label.textAlignment = NSTextAlignment.Center 
    label.text = "message" 
    label.textColor = UIColor.blackColor() 
    self.view.addSubview(label) 
0
 UILabel *mycoollabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)]; 

     [email protected]"I am cool";// 
    // for multiple lines,if text lenght is long use next line 

     mycoollabel.numberOfLines=0; 
     [self.View addSubView:mycoollabel]; 
Các vấn đề liên quan