2011-09-15 41 views
12

我做的代碼如下:程序化方式創建的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]; 

它看起來像this 但我希望它看起來像this。如何更改標籤的屬性?

回答

7

爲了顯示UILable爲你顯示你的形象,你需要設置的UILabel下列財產,同時提高標籤的高度。

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

應該像如下..

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

作爲SEP-05-2013 UILineBreakModeCharacterWrap其已過時的。相反,你可以使用label.lineBreakMode = NSLineBreakByCharWrapping,它可以完成與另一個相同的功能。 – Jiraheta

+0

謝謝,你可以編輯我的答案。 – Jhaliya

1

Set numberOfLines 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

如果你知道行號,即如果行數爲3,那麼你可以寫

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

和如果你不知道標籤的確切行,那麼你可以寫

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

設置您的標籤的Numberoflines屬性,然後也增加一些寬度的標籤,使您的標籤可以顯示正確。

此屬性控制爲了使標籤的文本適合其邊界矩形而使用的最大行數。此屬性的默認值爲1.要刪除任何最大限​​制並根據需要使用盡可能多的行,請將此屬性的值設置爲0.

如果您使用此屬性約束文本,符合最大行數,並使用適當的換行符模式截斷標籤的邊界矩形內。

read more

17

試試這個:

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

人使用NSTextAlignmentCenter爲UITextAlignmentCenter等棄用警告 – khunshan

2

一個很小的變化在iOS 6或更高的是,

label.textAlignment = UITextAlignmentCenter; 

已被棄用所以使用

label.textAlignment = NSTextAlignmentLeft; 

代替。

0

這裏是如何創建的UILabel編程..

1)寫這個在項目的.h文件中。

UILabel *label; 

2)將此寫入項目的.m文件中。

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

斯威夫特使用此,

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]; 
相關問題