Stretching bubble image in Chat app
Asked Answered
G

4

6

I am creating a chat app. I have to stretch the bubble image according to the text like other chatapps. I am using AutoLayout constraints. Size of imageview containing bubble increaser perfectly. (I have given yello background color to it). The bubble image is not getting stretched.

enter image description here

I have added these constraints:

enter image description here

For stretching image I have added this:

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

If anyone have idea about this, please respond. Thanks in advance.

Glossitis answered 6/1, 2017 at 7:39 Comment(3)
did you sliced the image in Assets.xcassets?Glossy
Yes, The same image slicing works in other code where everything is implemented programatically. I want to do the same with autolayouts. It doesn't work with autolayouts.Glossitis
Please use nini patch image github.com/shiami/SWNinePatchImageFactoryConcealment
C
0

First get the size of your text by 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 then get the height from the frame of text and put height into the imageView of the bubble. this all things done into the tableViewCell custom class into the -(void)layoutSubviews method.

Chickadee answered 6/1, 2017 at 8:21 Comment(0)
L
0

What you should do is to set the image in background of TextView so that it stretches as much the textview

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

You should always to use a "Resizable Bitmaps" better known as 9-patch images for this type of problem.

Leatherleaf answered 17/1, 2017 at 7:27 Comment(0)
S
0

Try this @SNarula

cell.bubbleImage.image = [[UIImage imageNamed:@"Bubble_Me"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20) resizingMode:UIImageResizingModeStretch];
Sickert answered 17/1, 2017 at 9:28 Comment(0)
A
0

try this one :

cell.bubbleImage.image = [[UIImage imageNamed:@"sender.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
Antibiosis answered 17/1, 2017 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.