NSAttributedString.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2019 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import <Foundation/Foundation.h>
  26. #import <WebKit/WKFoundation.h>
  27. #if TARGET_OS_IPHONE
  28. #import <UIKit/NSAttributedString.h>
  29. #else
  30. #import <AppKit/NSAttributedString.h>
  31. #endif
  32. @class NSAttributedString;
  33. NS_ASSUME_NONNULL_BEGIN
  34. /*!
  35. @abstract Indicates which local files WebKit can access when loading content.
  36. @discussion If NSReadAccessURLDocumentOption references a single file, only that file may be
  37. loaded by WebKit. If NSReadAccessURLDocumentOption references a directory, files inside that
  38. directory may be loaded by WebKit.
  39. */
  40. WK_EXTERN NSAttributedStringDocumentReadingOptionKey const NSReadAccessURLDocumentOption
  41. NS_SWIFT_NAME(readAccessURL) API_AVAILABLE(macos(10.15), ios(13.0));
  42. /*!
  43. @abstract Type definition for the completion handler block used to get asynchronous attributed strings.
  44. @discussion The completion handler block is passed the attributed string result along with any
  45. document-level attributes, like NSBackgroundColorDocumentAttribute, or an error. An implementation
  46. of this block type must expect to be called asynchronously when passed to HTML loading methods.
  47. */
  48. typedef void (^NSAttributedStringCompletionHandler)(NSAttributedString * _Nullable, NSDictionary<NSAttributedStringDocumentAttributeKey, id> * _Nullable, NSError * _Nullable)
  49. NS_SWIFT_NAME(NSAttributedString.CompletionHandler) API_AVAILABLE(macos(10.15), ios(13.0));
  50. /*!
  51. @discussion Extension of @link //apple_ref/occ/NSAttributedString NSAttributedString @/link to
  52. create attributed strings from HTML content using WebKit.
  53. */
  54. @interface NSAttributedString (NSAttributedStringWebKitAdditions)
  55. /*!
  56. @abstract Loads an HTML URL request and converts the contents into an attributed string.
  57. @param request The request specifying the URL to load.
  58. @param options Document attributes for interpreting the document contents.
  59. NSTextSizeMultiplierDocumentOption and NSTimeoutDocumentOption are supported option keys.
  60. @param completionHandler A block to invoke when the operation completes or fails.
  61. @discussion The completionHandler is passed the attributed string result along with any
  62. document-level attributes, or an error.
  63. */
  64. + (void)loadFromHTMLWithRequest:(NSURLRequest *)request options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options completionHandler:(NSAttributedStringCompletionHandler)completionHandler
  65. NS_SWIFT_NAME(loadFromHTML(request:options:completionHandler:)) API_AVAILABLE(macos(10.15), ios(13.0));
  66. /*!
  67. @abstract Converts a local HTML file into an attributed string.
  68. @param fileURL The file URL to load.
  69. @param options Document attributes for interpreting the document contents.
  70. NSTextSizeMultiplierDocumentOption, NSTimeoutDocumentOption and NSReadAccessURLDocumentOption
  71. are supported option keys.
  72. @param completionHandler A block to invoke when the operation completes or fails.
  73. @discussion The completionHandler is passed the attributed string result along with any
  74. document-level attributes, or an error. If NSReadAccessURLDocumentOption references a single file,
  75. only that file may be loaded by WebKit. If NSReadAccessURLDocumentOption references a directory,
  76. files inside that directory may be loaded by WebKit.
  77. */
  78. + (void)loadFromHTMLWithFileURL:(NSURL *)fileURL options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options completionHandler:(NSAttributedStringCompletionHandler)completionHandler
  79. NS_SWIFT_NAME(loadFromHTML(fileURL:options:completionHandler:)) API_AVAILABLE(macos(10.15), ios(13.0));
  80. /*!
  81. @abstract Converts an HTML string into an attributed string.
  82. @param string The HTML string to use as the contents.
  83. @param options Document attributes for interpreting the document contents.
  84. NSTextSizeMultiplierDocumentOption, NSTimeoutDocumentOption and NSBaseURLDocumentOption
  85. are supported option keys.
  86. @param completionHandler A block to invoke when the operation completes or fails.
  87. @discussion The completionHandler is passed the attributed string result along with any
  88. document-level attributes, or an error. NSBaseURLDocumentOption is used to resolve relative URLs
  89. within the document.
  90. */
  91. + (void)loadFromHTMLWithString:(NSString *)string options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options completionHandler:(NSAttributedStringCompletionHandler)completionHandler
  92. NS_SWIFT_NAME(loadFromHTML(string:options:completionHandler:)) API_AVAILABLE(macos(10.15), ios(13.0));
  93. /*!
  94. @abstract Converts HTML data into an attributed string.
  95. @param data The HTML data to use as the contents.
  96. @param options Document attributes for interpreting the document contents.
  97. NSTextSizeMultiplierDocumentOption, NSTimeoutDocumentOption, NSTextEncodingNameDocumentOption,
  98. and NSCharacterEncodingDocumentOption are supported option keys.
  99. @param completionHandler A block to invoke when the operation completes or fails.
  100. @discussion The completionHandler is passed the attributed string result along with any
  101. document-level attributes, or an error. If neither NSTextEncodingNameDocumentOption nor
  102. NSCharacterEncodingDocumentOption is supplied, a best-guess encoding is used.
  103. */
  104. + (void)loadFromHTMLWithData:(NSData *)data options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options completionHandler:(NSAttributedStringCompletionHandler)completionHandler
  105. NS_SWIFT_NAME(loadFromHTML(data:options:completionHandler:)) API_AVAILABLE(macos(10.15), ios(13.0));
  106. @end
  107. NS_ASSUME_NONNULL_END