Util.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Util.m
  3. // IBOSS
  4. //
  5. // Created by iHope on 14-6-6.
  6. // Copyright (c) 2014年 elongtian. All rights reserved.
  7. //
  8. #import "Util.h"
  9. @implementation Util
  10. +(void)alignLabelWithTop:(UILabel *)label{
  11. CGSize maxSize = CGSizeMake(label.frame.size.width, 999);
  12. label.adjustsFontSizeToFitWidth = NO;
  13. CGSize actualSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
  14. CGRect rect = label.frame;
  15. rect.size.height = actualSize.height;
  16. label.frame = rect;
  17. }
  18. +(NSString*)objectToJson:(id)_obj{
  19. if (_obj==nil) {
  20. return @"";
  21. }
  22. NSError *parseError = nil;
  23. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:_obj options:NSJSONWritingPrettyPrinted error:&parseError];
  24. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  25. }
  26. // 判断电话是否有效
  27. + (BOOL)isValidTelephone:(NSString *)telephone{
  28. /**
  29. * 手机号码
  30. * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,147
  31. * 联通:130,131,132,152,155,156,185,186
  32. * 电信:133,1349,153,180,181,189
  33. */
  34. NSString *mobile = @"^1(3[0-9]|5[0-35-9]|8[012345-9]|47|7[013678])\\d{8}$";
  35. /**
  36. * 大陆地区固话及小灵通 区号和电话之间没有-
  37. * 区号:010,020,021,022,023,024,025,027,028,029
  38. * 号码:七位或八位
  39. */
  40. NSString *phs = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  41. NSPredicate *predicatemobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobile];
  42. NSPredicate *predicatephs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phs];
  43. return [predicatemobile evaluateWithObject:telephone] || [predicatephs evaluateWithObject:telephone];
  44. }
  45. /**
  46. 返回Home区域的高度
  47. @return <#return value description#>
  48. */
  49. + (CGFloat) obtainHomeAreaHeight{
  50. return (SCREENHEIGHT >= 812 ? 34 : 0);
  51. }
  52. + (NSString *)positiveFormat:(NSString *)text{
  53. if(!text || [text floatValue] == 0){
  54. return @"¥0.00";
  55. }else{
  56. NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  57. [numberFormatter setPositiveFormat:@"¥,###.00;"];
  58. return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[text doubleValue]]];
  59. }
  60. return @"";
  61. }
  62. +(NSString*)thousandSeparatorFormat:(NSString *)number
  63. {
  64. if(!number || [number floatValue] == 0){
  65. return @"0.00";
  66. }else{
  67. NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  68. [numberFormatter setPositiveFormat:@",###.00;"];
  69. return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[number doubleValue]]];
  70. }
  71. return @"";
  72. }
  73. @end