How to add image and text in UITextView in IOS? -
i want add both text , image in uitextview. textview should expanded according length of text , image. in short want when capture image camera or pick gallery should display in uitextview , should able add text image similar facebook.i attaching image how uitextview like.
this absolutely possible now, using
+ (nsattributedstring *)attributedstringwithattachment:(nstextattachment *)attachment
see apple docs here
and example taken other answer:
uitextview *textview = [[uitextview alloc] initwithframe:cgrectmake(0,0,140,140)]; nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithstring:@"before after"]; nstextattachment *textattachment = [[nstextattachment alloc] init]; textattachment.image = [uiimage imagenamed:@"sample_image.jpg"]; cgfloat oldwidth = textattachment.image.size.width; //i'm subtracting 10px make image display nicely, accounting //for padding inside textview cgfloat scalefactor = oldwidth / (textview.frame.size.width - 10); textattachment.image = [uiimage imagewithcgimage:textattachment.image.cgimage scale:scalefactor orientation:uiimageorientationup]; nsattributedstring *attrstringwithimage = [nsattributedstring attributedstringwithattachment:textattachment]; [attributedstring replacecharactersinrange:nsmakerange(6, 1) withattributedstring:attrstringwithimage]; textview.attributedtext = attributedstring;
using above code image text inside uitextview on ios 7+. can/show style attributed text want , set width of image make sure fits within textview (as setting own aspect ratio/scale preference)
here's quick test image:
Comments
Post a Comment