iOS常用技术-气泡文本自适应

 1 //
 2 //  ChatBubble.h
 3 //  ChatBubble
 4 //
 5 //  Created by 大欢 on 16/1/21.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface ChatBubble : UIImageView
12 
13 //显示的文字
14 @property (nonatomic, copy) NSString * text;
15 
16 @end

/******************************************/

 1 //
 2 //  ChatBubble.m
 3 //  ChatBubble
 4 //
 5 //  Created by 大欢 on 16/1/21.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "ChatBubble.h"
10 
11 //文字距图片左边距
12 static const CGFloat kLeftMargin = 20;
13 
14 //文字距图片上,下,右边距
15 static const CGFloat kOtherMargin = 10;
16 
17 @interface ChatBubble ()
18 
19 @property (nonatomic, strong) UILabel * textLabel;
20 
21 @end
22 
23 @implementation ChatBubble
24 
25 - (UILabel *)textLabel {
26     
27     if (!_textLabel) {
28         _textLabel = [[UILabel alloc] init];
29         _textLabel.font = [UIFont systemFontOfSize:17];
30         _textLabel.textColor = [UIColor blackColor];
31         _textLabel.numberOfLines = 0;
32     }
33     return _textLabel;
34 }
35 
36 - (instancetype)initWithFrame:(CGRect)frame {
37     
38     if (self = [super initWithFrame:frame]) {
39         
40         //拉伸图片
41         self.image = [UIImage imageNamed:@"qipao"];
42         self.image = [self.image resizableImageWithCapInsets:UIEdgeInsetsMake(25, 15, 5, 5) resizingMode:UIImageResizingModeStretch];
43         //加载label
44         [self addSubview:self.textLabel];
45     }
46     return self;
47 }
48 
49 - (void)setText:(NSString *)text {
50     
51     _text = text;
52     _textLabel.text = text;
53 }
54 
55 - (CGSize)sizeThatFits:(CGSize)size {
56     
57     //计算Label的宽度
58     CGFloat width = size.width - kLeftMargin - kOtherMargin;
59     //根据Label的宽度计算出Label的高度
60     CGSize sizeLabel = [_textLabel sizeThatFits:CGSizeMake(width, MAXFLOAT)];
61     //设置Label的frame
62     _textLabel.frame = CGRectMake(kLeftMargin, kOtherMargin, sizeLabel.width, sizeLabel.height);
63     
64     CGFloat imageWidth = size.width;
65     //计算imageview的高度
66     CGFloat imageHeight = kOtherMargin * 2 + _textLabel.frame.size.height;
67 
68     return CGSizeMake(imageWidth, imageHeight);
69 }
70 
71 @end

/************************************************************/

 1 //
 2 //  ViewController.m
 3 //  ChatBubble
 4 //
 5 //  Created by 大欢 on 16/1/21.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "ChatBubble.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     
21     CGFloat width = self.view.frame.size.width - 60;
22     
23     ChatBubble * bubble = [[ChatBubble alloc] init];
24     bubble.text = @"初期投资66亿美元,最终长度为400英里,总投资330亿美元,折合每英里8250万美元。而超级高铁项目建设成本约为每英里2000万美元,😊";
25     CGSize size = [bubble sizeThatFits:CGSizeMake(width, MAXFLOAT)];
26     bubble.frame = CGRectMake(20, 30, size.width, size.height);
27     [self.view addSubview:bubble];
28     
29 }
30 
31 @end

/******************************************************/

posted @ 2016-01-21 23:21  MrWuYindi  阅读(449)  评论(0编辑  收藏  举报