NSBundle+YYAdd.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NSBundle+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/20.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provides extensions for `NSBundle` to get resource by @2x or @3x...
  15. Example: ico.png, ico@2x.png, ico@3x.png. Call scaledResource:@"ico" ofType:@"png"
  16. on iPhone6 will return "ico@2x.png"'s path.
  17. */
  18. @interface NSBundle (YYAdd)
  19. /**
  20. An array of NSNumber objects, shows the best order for path scale search.
  21. e.g. iPhone3GS:@[@1,@2,@3] iPhone5:@[@2,@3,@1] iPhone6 Plus:@[@3,@2,@1]
  22. */
  23. + (NSArray<NSNumber *> *)preferredScales;
  24. /**
  25. Returns the full pathname for the resource file identified by the specified
  26. name and extension and residing in a given bundle directory. It first search
  27. the file with current screen's scale (such as @2x), then search from higher
  28. scale to lower scale.
  29. @param name The name of a resource file contained in the directory
  30. specified by bundlePath.
  31. @param ext If extension is an empty string or nil, the extension is
  32. assumed not to exist and the file is the first file encountered that exactly matches name.
  33. @param bundlePath The path of a top-level bundle directory. This must be a
  34. valid path. For example, to specify the bundle directory for a Mac app, you
  35. might specify the path /Applications/MyApp.app.
  36. @return The full pathname for the resource file or nil if the file could not be
  37. located. This method also returns nil if the bundle specified by the bundlePath
  38. parameter does not exist or is not a readable directory.
  39. */
  40. + (nullable NSString *)pathForScaledResource:(NSString *)name
  41. ofType:(nullable NSString *)ext
  42. inDirectory:(NSString *)bundlePath;
  43. /**
  44. Returns the full pathname for the resource identified by the specified name and
  45. file extension. It first search the file with current screen's scale (such as @2x),
  46. then search from higher scale to lower scale.
  47. @param name The name of the resource file. If name is an empty string or
  48. nil, returns the first file encountered of the supplied type.
  49. @param ext If extension is an empty string or nil, the extension is
  50. assumed not to exist and the file is the first file encountered that exactly matches name.
  51. @return The full pathname for the resource file or nil if the file could not be located.
  52. */
  53. - (nullable NSString *)pathForScaledResource:(NSString *)name ofType:(nullable NSString *)ext;
  54. /**
  55. Returns the full pathname for the resource identified by the specified name and
  56. file extension and located in the specified bundle subdirectory. It first search
  57. the file with current screen's scale (such as @2x), then search from higher
  58. scale to lower scale.
  59. @param name The name of the resource file.
  60. @param ext If extension is an empty string or nil, all the files in
  61. subpath and its subdirectories are returned. If an extension is provided the
  62. subdirectories are not searched.
  63. @param subpath The name of the bundle subdirectory. Can be nil.
  64. @return The full pathname for the resource file or nil if the file could not be located.
  65. */
  66. - (nullable NSString *)pathForScaledResource:(NSString *)name
  67. ofType:(nullable NSString *)ext
  68. inDirectory:(nullable NSString *)subpath;
  69. @end
  70. NS_ASSUME_NONNULL_END