Clickable events in NSAttributedString in ios - ios

i had inserted UIimage inside uitextview as NSTextAttachment using nsattributedstring.
Now because of some default property for attrbutestring i am getting long press event on my image.
i.e When i do long press in UIImage it got UIActionsheet giving me option to "SAVE IMAGE" or "COPY".
I had not written any code for this..
What i actully want on the long press of UIIMAGE is i want to open that small UIIMAGE into my full screen like we have in whatapp like chatting.
When user send some image and user click on that image it get displayed in full screen.
Any one please help me to customise this click event of nstextattachment.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Somestring"];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(picture1);
UIImage *img=[UIImage imageWithData:imageData];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image =img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(0,0) withAttributedString:attrStringWithImage];
UITextView *messageContentView=[[UITextView alloc] init];
messageContentView.attributedText=attributedString;

Related

Sub Views are not visible after change the UILabel Background colour

I have changed my UILabel background colour like this. Actually I have added an image to it. I did it like this.
self.lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(8.0, self.lblDateTime.frame.origin.y+self.lblDateTime.frame.size.height, size.width-5.0, 100.0)];
UIImage *img = [UIImage imageNamed:#"msgBackground"];
CGSize imgSize = self.lblMsg.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.lblMsg.backgroundColor = [UIColor colorWithPatternImage:newImage];
[self.contentView addSubview:self.lblMsg];
Now I want to add another UILabelon to this and that label should include an image as an attachment. So I did it like this.
self.lblWeatherTitle=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.lblMsg.frame.size.width, 50.0)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"splashLogo"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[self.lblWeatherTitle setTextColor:[UIColor redColor]];
[myString appendAttributedString:attachmentString];
self.lblWeatherTitle.attributedText = myString;
[self.lblMsg addSubview:self.lblWeatherTitle];
But My 2nd UILabel is not visible.
This is what I need to do
This black text is a long description. As the title of it it should show those red label and the yellow icon. so I added that top label as a subview to this white label. This whole thing is inside a custom UITableViewCell
Please help me. How can I do this in correct way?
Thanks

Image unrecognized ios

I have an UIImage that I assign to an UIImageView.
When I do :
UIImage *picto = [UIImage imageNamed:#"animage.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
it works, but when I do :
UIImage *picto = [UIImage imageNamed:#"animage2.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
My image 2 isn't displayed, the grammar is correct and my image exist in the project...
By the way, the second image is not displayed just because it name
use this format :
UIImageView *icon_bg=[[UIImageView alloc]init];
icon_bg.image=[UIImage imageNamed:#"animage.png"];
[Cell.contentView addSubview:icon_bg];

How could you make a uilabel wrap around an image (like shown)

How could you achieve this effect:
Maybe some sort of NSAtributedString?
I have thought of hacky ways to just add spaces, but it needs to do it dynamically based on the width of the image.
Any ideas?
NOTE happily you can do this very easily with UITextView:
https://stackoverflow.com/a/20033752/294884
this question is about UILabel.
Add image in your label with text as below code:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Here is some text that is wrapping around like an image"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"first.jpg"];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString insertAttributedString:attrStringWithImage atIndex:0];
[_lbn setAttributedText:attributedString];
Your OUTPUT :
You can use an NSAttributedString with an NSTextAttachment
NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
[attachment setImage:<#UIImage#>];
NSAttributedString* icon = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:icon atIndex:<#index#>];
but it will only insert the image on one line, so it wont have multiple lines down the side of it (like a newspaper article), so its only useful for small images that fit on one line (sort of like how you have it in your question i guess)

ios7 custom interaction with nstextattachment in uilabel

I have an NSTextAttachment with an image in a UILabel and I would like to perform a custom behaviour when clicking on this attachment.
UITextViewDelegate provides an handy method
textView:shouldInteractWithTextAttachment:inRange:
But it can only be used if I'm using UITextView.
Is there any solution if Im using UILabel?
Thanks!
The text attachment becomes part of the model and is treated like a character, so try adding a link to the range of your attachment.
Like this:
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:#"myImage"];
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *mutableImageString = [imageString mutableCopy];
[mutableImageString addAttribute:NSLinkAttributeName value:#"http://stackoverflow.com" range:NSMakeRange(0, mutableImageString.length)];

Image not coming in UIImageView

I added a UIImageView to my application using an IBOutlet, then assigned an image programmatically, but the image is not displaying. My code is shown below:
NSString *string = #"Some text";
UIGraphicsBeginImageContext(CGSizeMake(180, 50));
[string drawAtPoint:CGPointMake(10, 20)
withFont:[UIFont systemFontOfSize:12]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[imageview setImage:result];
//[imageview
sample.image =result;

Resources