UILabel-使用总结

来源:互联网 发布:flac刻录cd软件 编辑:程序博客网 时间:2024/04/29 05:12



1.初始化一个UILabel


UILabel *lblType=[[UILabel alloc] initWithFrame:CGRectMake(90, 37, 30, 18)];

        [lblType setBackgroundColor:[UIColor clearColor]];

        [lblType setFont:[UIFont systemFontOfSize:10]];

        [lblType setTextColor:[UIColor colorWithRed:150/255.00 green:150/255.00blue:150/255.0 alpha:1.0]];

        [lblType setText:@"类型:"];

        [self.contentView addSubview:lblType];

        [lblType release];


2.UILabel为多行文本并且自适应高度

UIFont* theFont = [UIFont systemFontOfSize:14];

    CGSize sizeName = [[_dictInfoData objectForKey:@"dvdDesc"sizeWithFont:theFont

                          constrainedToSize:CGSizeMake(320.0MAXFLOAT)

                              lineBreakMode:NSLineBreakByWordWrapping];

    

    [_lblDescText setFrame:CGRectMake(0150320, sizeName.height)];

    [_lblDescText setNumberOfLines:0];

    [_lblDescText setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

    [_lblDescText setTextColor:[UIColor colorWithRed:51/255.0 green:128/255.0blue:123/255.0 alpha:1.0]];

    [_lblDescText setTextAlignment:NSTextAlignmentLeft];

    [_lblDescText setText:[_dictInfoData objectForKey:@"dvdDesc"]];

    [_lblDescText setBackgroundColor:[UIColor redColor]];

    [self.scrollView addSubview:_lblDescText];


3.UILabel为多行文本并且自适应宽度

这个就很少用了,吧,不过,要提一下,就是要跟上面的第2条差不多,将长度固定,就能得出宽度。


4.UILabel单行模式下,根据字体大小获得高度和宽度

我们知道,UILable控件默认是单行显示的。那么,如果需要在制定的字体下,获得要显示的文本的长度该如何做?

用法虽然不常见,但是,应该需要掌握。


 

UILabel *_lblDescText=[[UILabel allocinit];

UIFont* theFont = [UIFont systemFontOfSize:14];

[_lblDescText setFont:theFont];

[_lblDescText setText:@"为多行文本"];


 

[_lblDescText sizeToFit];// 显示文本需要的长度和宽度


    int height=_lblDescText.frame.size.height;

    int with=_lblDescText.frame.size.width;


5.UILabel单行模式下,自适应文字

这种情况用于,frame,一定,根据字数多少来自动显示,当文本多的时候,自动调整字体大小以适应UILable

[_lblText setAdjustsFontSizeToFitWidth:YES];




原创粉丝点击