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)];
Related
I have a label where I am trying to put a symbol image at the start of the label and then some text after it. This works, but the symbol image never changes size. It doesn't matter what size I provide in the UIImageSymbolConfiguration, it stays small. If I take this code and put the image in a UIImageView, then the image gets larger as expected. Is there something wrong with anything I am doing here related to the symbol image configuration?
UILabel *label = [[UILabel alloc] init];
NSString *title = #"Some Text";
label.adjustsFontForContentSizeCategory = YES;
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#" %#", title] attributes:#{
NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
NSForegroundColorAttributeName: [UIColor labelColor]
}];
UIImageSymbolConfiguration *configuration = [UIImageSymbolConfiguration configurationWithFont:[UIFont preferredFontForTextStyle:UIFontTextStyleLargeTitle]];
UIImage *squareImage = [[UIImage systemImageNamed:#"square.fill" withConfiguration:configuration] imageWithTintColor:[UIColor systemBlueColor]];
NSTextAttachment *imageAttachment = [NSTextAttachment textAttachmentWithImage:squareImage];
[string insertAttributedString:[NSAttributedString attributedStringWithAttachment:imageAttachment] atIndex:0];
label.attributedText = string;
I stumbled upon the same issue as I was building a similar feature today. It seems that symbols work slightly differently when embedded in text attachments: it's the font set on the attributed string that determines the size, not the configuration of the symbol itself.
With this in mind, you simply need to make sure the range of the icon in the attributed string has a font set:
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 1)];
Note that you'll still need to configure the symbol if you intend to render using a different color. In this case, attributed strings will ignore the NSForegroundColorAttributeName attribute when rendering the symbol (resulting in a blank, zero-width symbol). I suspect this is because symbols have hierarchical colors, but I might be wrong.
I'm trying to display an image in a UITextField using NSTextAttachment, but I want some horizontal space between the image and the text. However, when add the NSKernAttributeName attribute to the attributed string as follows, it resets the height of the attachment to the same height as the surrounding text.
var str = NSMutableAttributedString(attributedString: NSAttributedString(attachment: imageAttachment))
str.addAttribute(NSKernAttributeName, value: 10, range: NSRange(location: 0,length: 1))
Is there another way to add horizontal space between the image and the text?
The most direct way is in string start set a few space :
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setImage:[UIImage imageNamed:#"dest_poi_content_quotation"]];
NSString *reviewText = [NSString stringWithFormat:#" %#", review.text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:reviewText];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attrStringWithImage atIndex:0];
[self.lblComment setAttributedText:attributedString];
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;
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)
So i've got a UITextView the contents of which are loaded in from an RTF file. I have the data detector type set to UIDataDetectorTypeLink, it looks as follows:
cell.articleView.attributedText = attrString;
cell.articleView.dataDetectorTypes = UIDataDetectorTypeLink;
cell.articleView.userInteractionEnabled = YES;
cell.articleView.selectable = YES;
My attributed string is:
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[NSData dataWithContentsOfFile:path]
options:#{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} documentAttributes:Nil error:nil];
In the cell the links load, they're colored blue etc, but clicking on them does nothing. Im assuming its because the Cell is intercepting the clicks, but I'm not sure how to circumvent that.