Stretching a bubble image in a chat application

I am creating a chat application. I have to stretch the bubble image to fit the text, like other chatapps . I use AutoLayout restrictions. The image size containing the bubble booster is excellent. (I gave the background color to yello). The bubble image does not stretch.

enter image description here

I have added these restrictions:

enter image description here

To stretch the image, I added the following:

 let myImage = self.imageNamed("Bubble_Me") let new_Image = myImage.stretchableImage(withLeftCapWidth: 15, topCapHeight: 14) cell.bubbleImage.image = new_Image 

If anyone has an idea about this, answer. Thanks in advance.

+6
source share
4 answers

First get the size of your text using this.

 UIFont *font = [UIFont systemFontOfSize:14.0]; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:message attributes:attrsDictionary]; CGRect paragraphRect = [attrString boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 20 , CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil]; 

After you get the frame of your text, get the height from the frame of the text and put the height in the image View of the bubble. all this is done in the tableViewCell custom class in -(void)layoutSubviews method.

0
source

What you need to do is set the image in the background of the TextView so that it stretches like a text image

 text.backgroundColor = UIColor(patternImage: UIImage(named:"bg.png")!) 

You should always use โ€œMutable Bitmapsโ€, better known as 9-patch images for this type of problem.

0
source

Try @SNarula

 cell.bubbleImage.image = [[UIImage imageNamed:@"Bubble_Me"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20) resizingMode:UIImageResizingModeStretch]; 
0
source

try the following:

 cell.bubbleImage.image = [[UIImage imageNamed:@"sender.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2]; 
0
source

Source: https://habr.com/ru/post/1013818/


All Articles