Ios imitation WeChat, text message chat bubbles

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.