3

i am getting the NSAttributedString from some applications with the help of NSPasteboard (when someone copy). The font in the NSAttributedString is always good but sometimes (depending on the application), when i convert to HTML, i get a different font and size. For example : Calibri goes to Helvetica. Here is my code :

  //Get pasteboard nsattributedstring
   NSArray *classes = [[NSArray alloc] initWithObjects:[NSAttributedString class], nil];
   NSDictionary *options = [[NSDictionary alloc] init];
   BOOL ok = [pasteboard canReadObjectForClasses:classes options:options];
   if(ok){
       NSArray *copiedItems = [pasteboard readObjectsForClasses:classes options:options];
       if (copiedItems != nil) {
           NSAttributedString *stringTest = [copiedItems lastObject];
           NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
           NSData *htmlData = [stringTest dataFromRange:NSMakeRange(0, stringTest.length) documentAttributes:documentAttributes error:NULL];
           htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

       }
   }

I get my NSAttributedString from the pasteboard which is fine, then i convert it to HTML. It is when i convert it to HTML the problem, it changes the font. For example, i use Calibri, MacOS doesn't have this font so it won't work.

**This is on MacOS

hugo411
  • 320
  • 1
  • 12
  • 29
  • Regarding the size: https://stackoverflow.com/questions/51560409/html-to-attributed-string-to-html-increase-font-size-for-random-text – Larme Aug 20 '18 at 13:06
  • 3
    What do you expect to happen if the font isn’t available? – Christian Schnorr Aug 22 '18 at 19:48
  • I know, looking for a solution for that. Even if Calibri doesn't exist in mac, the NSAttributedString still writes Calibri but the conversion to HTML transforms it to Helvetica. I also got different results in google docs for safari and chrome which is strange. – hugo411 Aug 22 '18 at 19:56
  • I came across something like this and was able to use my desired font by adding the font as attribute to the Attributed String, have you tried it? – daris mathew Aug 29 '18 at 04:37
  • Hey thanks, even if you don't have the font it works? For example Calibri is not there on macos, the attributedstring is saying Helvetica instead. Ill try adding the NSFont attribute Calibri. – hugo411 Aug 29 '18 at 19:50
  • When adding a font attribute, i get a nil error because it doesn't know anything about Calibri. – hugo411 Aug 29 '18 at 20:08

0 Answers0