UILabel范例实现代码如下
Posted 旅途
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UILabel范例实现代码如下相关的知识,希望对你有一定的参考价值。
#import "TWO_ViewController.h"
#define SCREEN_Width [[UIScreen mainScreen] bounds].size.width
@interface TWO_ViewController ()
@end
@implementation TWO_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
UIView *headerView = [[UIView alloc] init];
UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, SCREEN_Width - 40, 20)];
image1.backgroundColor = [UIColor redColor];
UILabel *ddlabel = [[UILabel alloc] init];
[ddlabel setText:@"根据文字 高度进行适应根据文字高度 进行自适应根据文适应 根据文字高度进行自适应根据文自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应"];
ddlabel.numberOfLines = 4;
ddlabel.font = [UIFont systemFontOfSize:24];
CGRect rect = [ddlabel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: ddlabel.font} context:nil];
ddlabel.frame = CGRectMake(20, CGRectGetMaxY(image1.frame),SCREEN_Width - 40,rect.size.height);
[self setLineSpacing:20 label:ddlabel];
//[ddlabel sizeToFit];
UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(ddlabel.frame), SCREEN_Width - 40, 20)];
image2.backgroundColor = [UIColor blueColor];
headerView.frame = CGRectMake(0, 64, SCREEN_Width, CGRectGetMaxY(image2.frame)+20);
NSLog(@"test height = %f", ddlabel.frame.size.height);
headerView.backgroundColor = [UIColor whiteColor];
[headerView addSubview:image1];
[headerView addSubview:ddlabel];
[headerView addSubview:image2];
[self.view addSubview:headerView];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label
{
if(!label.text.length) return;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:spacing];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
[label setAttributedText:attributedString];
label.lineBreakMode = NSLineBreakByTruncatingTail;
[label sizeToFit];
//[attributeString setAttributes:@{NSForegroundColorAttributeName:NF_Color_C19,NSFontAttributeName:[UIFont systemFontOfSize:Near_Final_Font_T9]} range:NSMakeRange(i, 1)];
}
@end
以上是关于UILabel范例实现代码如下的主要内容,如果未能解决你的问题,请参考以下文章
iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段