Is it possible to insert an image (not a background image) into an NSTextView?
Something like:
Hi :) How are you?
and it should display a "smiley" image. I have an NSTextView and an NSImage.
Insert NSImage into NSTextView:
NSImage * pic = [[NSImage alloc] initWithContentsOfFile:#"/Users/Anne/Desktop/Sample.png"];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment: attachment];
[[textView textStorage] appendAttributedString:attributedString];
In the header file:
IBOutlet id textView;
Related
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
NSImage *image = [NSImage imageNamed:#"emotion"];;
attachment.image = image;
NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment: attachment];
[[_feedbackContent textStorage] appendAttributedString:attributedString];
after add the image to NSTextAttachment, it is vertical flipped. Anybody know how to resolve this issue.
NSTextAttachment seems to use NSFileWrapper filename to get the UTI and has different behaviour based on the UTI.
I was able to fix it with using NSFileWrapper instead:
NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
// Without a filename (which is used to get the UTI by NSTextAttachment)
// the image is displayed flipped.
[fileWrapper setPreferredFilename:#"Attachment.png"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
You could also try to set the fileType property to kUTTypePNG or other image type to get it working.
radar://47170950
Assign the image to an NSTextAttachmentCell, not the NSTextAttachment.
id <NSTextAttachmentCell> cell = [[NSTextAttachmentCell alloc] initImageCell:image];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
[attachment setAttachmentCell:cell];
When drawing into a flipped view (which is why the image gets flipped), the only workaround I've found is to create a flipped image so it's then drawn the :
textAttachment.image = [NSImage imageWithSize:image.size flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
[image drawInRect:dstRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
return YES;
}];
How to display html content in label.
Bellow link is how my label display html content.
https://i.stack.imgur.com/j1TKP.png
I also try using this code.
NSString * htmlString = #"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;
I tried your html string which was in image.I got the solution
ViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString * htmlString = #"Description: The popular Mini Pashili holds your daily essentials. With signature side zip-pleats,this style is often worn fanned-open and cross body via detachable shoulder strap.Just pinch to open the push-lock padlock.<ul><li>100%Full Grain Leather</li><li>6.5”H x 8.5”W x 3”D</li><li>16.5”-22” Adjustable Shoulder Starp</li><li>Fully Lined</li><li>Dust Bag included</li><li>Imported</li><li>AE17-0226SKC</li></ul>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(200, 200, 350, 300);
myLabel.numberOfLines = 4;
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];
}
Now the solution is
I wrote the following and no image comes up. I displayed the pointer and it comes out as "0".
In the next code block I checked the path and made shore the image was present there. Still nothing loads.
NSImageView *vi = [[NSImageView alloc] initWithFrame:CGRectMake(10, 10, 220, 220)];
[self.view addSubview:vi];
NSImage *dog = [NSImage imageNamed:#"airnow3.jpg"];
NSLog(#"pointer %d", dog);
[vi setImage:dog];
NSFileManager *filemgr;
NSString *currentpath;
filemgr = [[NSFileManager alloc] init];
currentpath = [filemgr currentDirectoryPath];
NSLog(#"%#", currentpath);
Why don't you use InitWithImage:(NSImage) instead of InitWithFrame
Maybe you should use UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(10,10,220,220)];
UIImage *image = [UIImage imageNamed:#"airnow3.jpg"];
[imageView setImage: image];
[self.view addSubview: imageView];
I've got an NSButton with an attached CALayer and an image set on it. When I click the button, the image flips!
_zoomButton = [NSButton new];
[_zoomButton setBordered:NO];
[_zoomButton setButtonType:NSMomentaryChangeButton];
[_zoomButton setTitle:#""];
[_zoomButton setWantsLayer:YES];
[_zoomButton setImage:s_zoomImage];
[_zoomButton sizeToFit];
s_zoomImage is just an NSImage loaded from resources:
namespace
{
NSImage *s_zoomImage; // No static variables in Objective-C.
}
+ (void)initialize
{
[super initialize];
NSString *const zoomImagePath = [[NSBundle mainBundle] pathForResource:#"Zoom.png" ofType:nil];
s_zoomImage = [[[NSImage alloc] initWithContentsOfFile:zoomImagePath] autorelease];
}
At first the button displays correctly, but if I click it, the image (s_zoomImage) flips vertically! Any ideas?
In my application (code listed below), I use a popover to display a series of colors that the user can choose. These colors are used for the color of the drawing they are completing above. I am trying to modify the popover to work the same way, except for this time I would want to display images (the images are saved in the application's documents folder as png files) instead of blocks of color. Listed below is the working code for the color selector popover. ColorGrid is a UIview which contains an NSArray Colors, as well as two NSUIntegers columnCount and rowCount. I have tried to replace the items in the colors array with UIImages of the png files, as well as UIImageViews but I have not been able to get a successful result (or a compilable one). Listed below is the working code. Could anyone show me how I can change the UIColor items to the images to show them in the grid?
- (IBAction)popoverStrokeColor:(id)sender {
StrokeColorController *scc = [[[StrokeColorController alloc] initWithNibName:#"SelectColorController" bundle:nil] autorelease];
scc.selectedColor = self.strokeColor;
[self doPopoverSelectColorController:scc sender:sender];
}
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
scc.colorGrid.colors = [NSArray arrayWithObjects:
//put the following below back in after testing
[UIColor blackColor],
[UIColor blueColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor orangeColor],
//[UIColor purpleColor],
// [UIColor brownColor],
// [UIColor whiteColor],
// [UIColor lightGrayColor],
//[UIColor cyanColor],
//[UIColor magentaColor],
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Thanks for your help. I am new to objective-c.
edit - heres the function with my attempt to insert the images instead of the colors
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
// these have to be set after the view is already loaded (which happened
// a couple of lines ago, thanks to scc.view...
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
//here we need to get the UIImage items to try to put in the array.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
//[fileName appendString:#".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:#"test.png"];
NSString *strFile1 = [documentDirectory stringByAppendingPathComponent:#"test1.png"];
NSString *strFile2 = [documentDirectory stringByAppendingPathComponent:#"test2.png"];
NSString *strFile3 = [documentDirectory stringByAppendingPathComponent:#"test3.png"];
NSString *strFile4 = [documentDirectory stringByAppendingPathComponent:#"test4.png"];
NSString *strFile5 = [documentDirectory stringByAppendingPathComponent:#"test5.png"];
//now for the Images
UIImage *image = [ UIImage imageWithContentsOfFile: strFile];
UIImage *image1 = [ UIImage imageWithContentsOfFile: strFile1];
UIImage *image2 = [ UIImage imageWithContentsOfFile: strFile2];
UIImage *image3 = [ UIImage imageWithContentsOfFile: strFile3];
UIImage *image4 = [ UIImage imageWithContentsOfFile: strFile4];
UIImage *image5 = [ UIImage imageWithContentsOfFile: strFile5];
UIImageView *imageview = [[[UIImageView alloc] initWithImage:image] autorelease];
[self.view addSubview:imageview];
UIImageView *imageview1 = [[[UIImageView alloc] initWithImage:image1] autorelease];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[[UIImageView alloc] initWithImage:image2] autorelease];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[[UIImageView alloc] initWithImage:image3] autorelease];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[[UIImageView alloc] initWithImage:image4] autorelease];
[self.view addSubview:imageview4];
UIImageView *imageview5 = [[[UIImageView alloc] initWithImage:image5] autorelease];
[self.view addSubview:imageview5];
imageview.image = image;
imageview1.image = image1;
imageview2.image = image2;
imageview3.image = image3;
imageview4.image = image4;
imageview5.image = image5;
scc.colorGrid.colors = [NSArray arrayWithObjects:
// When attempting to add the images like this - get the error identified expected
// after the e in image, at the end bracket. Putting a * does nothing to change the error
[image],
// When adding one of the Imageviews, i get the same error as above
//below is how I attempted to add it
[imageView],
//
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Remove your square brackets around image and/or imageView :
scc.colorGrid.colors = [NSArray arrayWithObjects:
// Not : [image] but
image,
// or
imageView,
// Not : [imageView],
nil];