开发者

create programmatically an UILabel

I did by code the following:

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

And it looks like this but I want it to look like this. How to change the label's properties?


Try this:

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


To show the UILable as your displayed in your image, you need to set the following property of UILabel and also increase the height of your Label.

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

Should be like as below ..

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


if you know the number of line i.e if number of Line is 3 then you can write

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

and if u don't know the exact line for label then you can write

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


One Minor Change on iOS 6 or later is that the

label.textAlignment = UITextAlignmentCenter;

is deprecated so use

label.textAlignment = NSTextAlignmentLeft;

instead.


Set numberOfLines property of UILabel.

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


Set Numberoflines property of your label and then also increase some width of your lable so your label can shows proper.

This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.

If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.

read more


In Swift use this,

    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)


here is how to create UILabel Programmatically..

1) Write this in .h file of your project.

UILabel *label;

2) Write this in .m file of your project.

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. 


      UILabel *mycoollabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];

      mycoollabel.text=@"I am cool";//
     // for multiple lines,if text lenght is long use next line

      mycoollabel.numberOfLines=0;
      [self.View addSubView:mycoollabel];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜