您的位置:首页 > 产品设计 > UI/UE

iOS-UIKit(UILabel.h常用技巧1(随字体多行后的高度,渐变字体Label,自适应高度,添加边框))

2015-10-28 11:28 441 查看
1、计算UIlabel随字体多行后的高度

CGRect bounds = CGRectMake(0,
0, 200, 300);

CGRect heightLabel = [myLabel textRectForBounds:bounds

limitedToNumberOfLines:20];
//计算20行后的Label的Frame

NSLog(@"%f",heightLabel.size.height);

2、UILabel根据字数多少自动实现适应高度

UILabel *msgLabel = [[UILabel alloc]

initWithFrame:CGRectMake(15,
45, 0, 0)];

msgLabel.backgroundColor = [UIColor lightTextColor];

[msgLabel setNumberOfLines:0];

msgLabel.lineBreakMode = UILineBreakModeWordWrap;

msgLabel.font = [UIFont fontWithName:@"Arial" size:12];

CGSize size = CGSizeMake(290,1000);

msgLabel.text = @"获取到的deviceToken,我们可以通过webservice服务提

交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使

用。";

CGSize msgSie = [msgLabel.text sizeWithFont:fonts

constrainedToSize:size];

[msgLabel setFrame:CGRectMake(15,
45, 290, msgSie.height)];

3、渐变字体Label

UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage

imageNamed:@"btn.png"]];

NSString *title =
@"Setting";

UILabel *titleLabel = [[UILabelalloc]

initWithFrame:CGRectMake(0,0,
80,44)];

titleLabel.textColor = titleColor;

titleLabel.text = title;

titleLabel.font = [UIFont boldSystemFontOfSize:20];

titleLabel.backgroundColor = [UIColor clearColor];

[self.view addSubview:titleLabel];

[titleLabel release];

4、Label添加边框

titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];

titleLabel.layer.borderWidth = 2;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: