效果图: 数据用的就是小马哥的plist 气泡也是 我这个是纯代码
旧的 -_-!! 朋友说没模型 cell高度也没计算 这个是修改版的样子
code1 这个是旧版
NSString * str = @"赵大财10秒让你学会写IOS消息气泡 大财10秒让你学会写IOS消息气泡 大财10秒让你学会写IOS消息气泡" ;
[self.contentView addSubview:self.btn];
[self.btn autoSetDimension:ALDimensionWidth toSize:200]; //首先设置宽度 这里用的是第三方约束
[self.btn setTitle:str forState:UIControlStateNormal];
self.btn.titleLabel.font = [UIFont systemFontOfSize:14];
self.btn.titleLabel.numberOfLines = 0;
[self.btn layoutIfNeeded]; //这里刷新下 不然得不到高度
[self.btn autoSetDimension:ALDimensionHeight toSize:self.btn.titleLabel.frame.size.height+40];
[self.btn autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
[self.btn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:40];
self.btn.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 20);
UIImage *img = [UIImage imageNamed:@"chat_send_nor"];
img = [img stretchableImageWithLeftCapWidth:img.size.width*0.5 topCapHeight:img.size.height*0.5];// 这里居中拉伸
[self.btn setBackgroundImage:img forState:UIControlStateNormal];
self.contentView.backgroundColor = RGBRandomColor;
code 2 这个是新版
CGSize titleSize =[messageModel.text sizeWithFontWidth:[UIFont systemFontOfSize:15]];
CGFloat paowidth = titleSize.width>=200?200:titleSize.width;
[self.contentView addSubview:self.btn];
self.btn.width = paowidth+40; //这里 都换成frame形式了
[self.btn setTitle:messageModel.text forState:UIControlStateNormal];
self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
self.btn.titleLabel.numberOfLines = 0;
[self.btn layoutIfNeeded]; //这里刷新下 不然得不到高度
self.btn.height = self.btn.titleLabel.frame.size.height+30;
self.btn.x= YLScreenW - paowidth - 40;
self.btn.y =0;
UIImage *img = [UIImage imageNamed:@"chat_send_nor"];
img = [img stretchableImageWithLeftCapWidth:img.size.width*0.5 topCapHeight:img.size.height*0.5];// 这里居中拉伸
[self.btn setBackgroundImage:img forState:UIControlStateNormal];
self.contentView.backgroundColor = RGBRandomColor;
messageModel.cellHeight = self.btn.titleLabel.frame.size.height+30;
model:
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *time;
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
VC:
#import "messageTableViewCell.h"
@interface MeIndexViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong ,nonatomic)UITableView *tableView;
@property (strong,nonatomic) NSArray *dacaiMessageModel;
@end
@implementation MeIndexViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor YLColorGrayBg];
self.navigationItem.title = @"我的";
[self.view addSubview:self.tableView];
[self.tableView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
_dacaiMessageModel = [messageModel mj_objectArrayWithKeyValuesArray:dictArray];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dacaiMessageModel.count;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//[messageTableViewCell ]
messageModel * model = _dacaiMessageModel[indexPath.row];
return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
messageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMessageTableViewCellCellIdentifier forIndexPath:indexPath];
[cell daCaiHeight:_dacaiMessageModel[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
[_tableView configureForAutoLayout];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.alwaysBounceVertical = YES;
[_tableView registerClass:[messageTableViewCell class] forCellReuseIdentifier:kMessageTableViewCellCellIdentifier];
}
return _tableView;
}