SLMethod.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // SLMethod.m
  3. // DarkMode
  4. //
  5. // Created by wsl on 2020/4/24.
  6. // Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved.
  7. //
  8. #import "SLMethod.h"
  9. @implementation SLMethod
  10. + (void)userDefaultsSetObject:(nullable id)value forKey:(NSString *)key {
  11. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  12. NSDictionary *dict = @{key:value};
  13. [userDefault setObject:dict forKey:SLUserDefaultsKey];
  14. [userDefault synchronize];
  15. }
  16. + (id)userDefaultsObjectForKey:(NSString *)key {
  17. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  18. NSDictionary *dict = [userDefault objectForKey:SLUserDefaultsKey];
  19. return dict[key];
  20. }
  21. /// 动态计算文字的宽高
  22. + (CGSize)sizeFromText:(NSString *)text textFont:(UIFont *)font maxSize:(CGSize)maxSize {
  23. if(text == nil || text == NULL || [text isKindOfClass:[NSNull class]] || [[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
  24. return CGSizeZero;
  25. }
  26. NSDictionary *attrs = @{NSFontAttributeName:font};
  27. CGSize size = [text boundingRectWithSize:CGSizeMake(maxSize.width, maxSize.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
  28. return CGSizeMake(ceil(size.width), ceil(size.height));
  29. }
  30. + (CGSize)sizeFromAttributedText:(NSAttributedString *)attributedText maxSize:(CGSize)maxSize
  31. {
  32. if (attributedText == nil) {
  33. return CGSizeZero;
  34. }
  35. CGSize size = [attributedText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size;
  36. return CGSizeMake(ceil(size.width), (ceil(size.height)+1));
  37. }
  38. /// 切四个不同半径圆角的函数
  39. /// @param bounds 区域
  40. /// @param cornerRadii 四个圆角的半径
  41. + (CGPathRef)cornerPathCreateWithRoundedRect:(CGRect)bounds cornerRadii:(SLCornerRadii)cornerRadii {
  42. const CGFloat minX = CGRectGetMinX(bounds);
  43. const CGFloat minY = CGRectGetMinY(bounds);
  44. const CGFloat maxX = CGRectGetMaxX(bounds);
  45. const CGFloat maxY = CGRectGetMaxY(bounds);
  46. const CGFloat topLeftCenterX = minX + cornerRadii.topLeft;
  47. const CGFloat topLeftCenterY = minY + cornerRadii.topLeft;
  48. const CGFloat topRightCenterX = maxX - cornerRadii.topRight;
  49. const CGFloat topRightCenterY = minY + cornerRadii.topRight;
  50. const CGFloat bottomLeftCenterX = minX + cornerRadii.bottomLeft;
  51. const CGFloat bottomLeftCenterY = maxY - cornerRadii.bottomLeft;
  52. const CGFloat bottomRightCenterX = maxX - cornerRadii.bottomRight;
  53. const CGFloat bottomRightCenterY = maxY - cornerRadii.bottomRight;
  54. //虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针
  55. CGMutablePathRef path = CGPathCreateMutable();
  56. //顶 左
  57. CGPathAddArc(path, NULL, topLeftCenterX, topLeftCenterY,cornerRadii.topLeft, M_PI, 3 * M_PI_2, NO);
  58. //顶 右
  59. CGPathAddArc(path, NULL, topRightCenterX , topRightCenterY, cornerRadii.topRight, 3 * M_PI_2, 0, NO);
  60. //底 右
  61. CGPathAddArc(path, NULL, bottomRightCenterX, bottomRightCenterY, cornerRadii.bottomRight,0, M_PI_2, NO);
  62. //底 左
  63. CGPathAddArc(path, NULL, bottomLeftCenterX, bottomLeftCenterY, cornerRadii.bottomLeft, M_PI_2,M_PI, NO);
  64. CGPathCloseSubpath(path);
  65. return path;
  66. }
  67. @end