天天省钱快报

NSString+CCFunction.m 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // NSString+CCFunction.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/25.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "NSString+CCFunction.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @implementation NSString (CCFunction)
  11. - (CGSize)stringSizeWithFont:(UIFont *)font{
  12. return [self sizeWithAttributes:@{NSFontAttributeName:font}];
  13. }
  14. - (NSUInteger)getBytesLength
  15. {
  16. NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  17. return [self lengthOfBytesUsingEncoding:enc];
  18. }
  19. - (NSString *)stringByDeletingPictureResolution{
  20. NSString *doubleResolution = @"@2x";
  21. NSString *tribleResolution = @"@3x";
  22. NSString *fileName = self.stringByDeletingPathExtension;
  23. NSString *res = [self copy];
  24. if ([fileName hasSuffix:doubleResolution] || [fileName hasSuffix:tribleResolution]) {
  25. res = [fileName substringToIndex:fileName.length - 3];
  26. if (self.pathExtension.length) {
  27. res = [res stringByAppendingPathExtension:self.pathExtension];
  28. }
  29. }
  30. return res;
  31. }
  32. //房间号,纯数字
  33. + (BOOL)checkRoomNumber:(NSString *)roomNumber
  34. {
  35. NSString *pattern =@"^[0-9]*$";
  36. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
  37. BOOL isMatch = [pred evaluateWithObject:roomNumber];
  38. return isMatch;
  39. }
  40. + (BOOL)checkPullUrl: (NSString *) pullUrl
  41. {
  42. BOOL isMatch = YES;
  43. if (pullUrl == nil || pullUrl.length == 0) {
  44. isMatch = NO;
  45. }
  46. if (![pullUrl hasPrefix:@"http://"] && ![pullUrl hasPrefix:@"rtmp://"]) {
  47. isMatch = NO;
  48. }
  49. return isMatch;
  50. }
  51. //1-20位数字或者字母
  52. + (BOOL)checkUserName:(NSString*) username
  53. {
  54. NSString *pattern =@"^[A-Za-z0-9]{1,20}$";
  55. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
  56. BOOL isMatch = [pred evaluateWithObject:username];
  57. return isMatch;
  58. }
  59. //6-20位字母或数字
  60. + (BOOL)checkPassword:(NSString*) password
  61. {
  62. NSString *pattern =@"^[A-Za-z0-9]{6,20}$";
  63. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
  64. BOOL isMatch = [pred evaluateWithObject:password];
  65. return isMatch;
  66. }
  67. //6-20位数字或者字母
  68. + (BOOL)checkNickName : (NSString*) nickName
  69. {
  70. NSString *pattern =@"^[\u4E00-\u9FA5A-Za-z0-9]{1,10}$";
  71. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
  72. BOOL isMatch = [pred evaluateWithObject:nickName];
  73. return isMatch;
  74. }
  75. - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
  76. {
  77. NSDictionary *dict = @{NSFontAttributeName: font};
  78. CGSize textSize = [self boundingRectWithSize:maxSize
  79. options:NSStringDrawingUsesLineFragmentOrigin
  80. attributes:dict
  81. context:nil].size;
  82. return textSize;
  83. }
  84. @end