The chat bubbles and the chat bubbles of Apple Text messages have always been very classic. A small bubble increases and decreases according to the amount of text.
In fact, the implementation is very simple, mainly because of the adaptive height of the control. Here we use cell.
Core code
-(UIView *) bubbleView :( NSString *) textimageName :( NSString *) name
{
UIView * returnView = [[UIViewalloc] initWithFrame: CGRectZero];
UIImage * bubble;
ReturnView. backgroundColor = [UIColorclearColor]; // ImageBubble @ 2x ~ Iphone
If ([nameisdomaintostring: @ "1"]) {// bubble-default-outgoing @ 2x
Bubble = [[Signature: [NSBundlemainBundle] pathForResource: @ "bubble-default-incoming-green @ 2x" ofType: @ "png"] Signature: UIEdgeInsetsMake (15.0f, 25366f, 16.0f, 23.0f)];
} Else {
Bubble = [[UIImageimageNamed: @ "ImageBubble ~ Iphone "] stretchableImageWithLeftCapWidth: 15 topCapHeight: 14];
}
UIImageView * bubbleImageView = [[UIImageViewalloc] initWithImage: bubble];
UIFont * font = [UIFontsystemFontOfSize: 13];
CGSize size = [textsizeWithFont: fontconstrainedToSize: CGSizeMake (220366f, 1000.0f) lineBreakMode: NSLineBreakByWordWrapping];
CGSize new1 = [textsizeWithFont: fontconstrainedToSize: CGSizeMake (220366f, size. height) lineBreakMode: NSLineBreakByWordWrapping];
UILabel * bubbleText;
If ([nameisdomaintostring: @ "1"]) {
BubbleText = [[UILabel alloc] initWithFrame: CGRectMake (12.0f, 5.0f, new1.width + 10, new1.height + 10)];
} Else {
BubbleText = [[UILabel alloc] initWithFrame: CGRectMake (5.0f, 5.0f, new1.width + 10, new1.height + 10)];
}
BubbleText. backgroundColor = [UIColorclearColor];
BubbleText. font = font;
BubbleText. numberOfLines = 0;
BubbleText. lineBreakMode = NSLineBreakByWordWrapping;
BubbleText. text = text;
BubbleImageView. frame = CGRectMake (0.0f, 0.0f, new1.width + 20, new1.height + 2020.f );
If ([nameisdomaintostring: @ "1"]) {
ReturnView. frame = CGRectMake (40366f, 30366f, new1.width + 20, new1.height + 20366f );
} Else {
ReturnView. frame = CGRectMake (2601_f-new1.width, 401_f, new1.width + 20, new1.height + 201_f );
}
[ReturnView addSubview: bubbleImageView];
[ReturnView addSubview: bubbleText];
ReturnreturnView;
}
This code can be used directly, and the cell adaptive height must be realized in the tableview proxy method.
-(CGFloat) tableView :( UITableView *) tableViewheightForRowAtIndexPath :( NSIndexPath *) indexPath
Finally, I would like to emphasize that many similar demos on the internet can achieve this kind of effect. However, some demos are used to find that the bubble is blowing and deformation. This is not a problem in the method, but an image problem, try to find more bubbles. Lz has been plagued by this problem for a long time, and found through several versions