App開發流程之字串處理工具類,app工具類
記錄字串的處理,不是一個簡單的工作。
NSString是代碼中隨處可見的類型,也是應用和處理繁多的對象,在此只記錄需要常備的方法,並且加以說明。
#pragma mark -- 【計算字串尺寸+ (CGSize)getStringSizeWith:(NSString *)string attributes:(NSDictionary *)attributes;+ (CGSize)getStringSizeWith:(NSString *)string attributes:(NSDictionary *)attributes maxWidth:(CGFloat)maxWidth;+ (CGSize)getStringSizeWith:(NSString *)string attributes:(NSDictionary *)attributes maxHeight:(CGFloat)maxHeight;+ (CGSize)getStringSizeWith:(NSString *)string font:(UIFont *)font paragraphStyle:(NSParagraphStyle *)paragraphStyle maxWidth:(CGFloat)maxWidth;+ (CGSize)getStringSizeWith:(NSString *)string font:(UIFont *)font lineHeight:(CGFloat)lineHeight maxWidth:(CGFloat)maxWidth;+ (CGSize)getStringSizeWith:(NSString *)string font:(UIFont *)font;+ (CGSize)getStringSizeWith:(NSString *)string font:(UIFont *)font maxWidth:(CGFloat)maxWidth;+ (CGSize)getStringSizeWith:(NSString *)string font:(UIFont *)font maxHeight:(CGFloat)maxHeight;#pragma mark -- 】計算字串尺寸#pragma mark -- 【產生屬性字串+ (NSAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color lineHeight:(CGFloat)lineHeight maxWidth:(CGFloat)maxWidth;+ (NSAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color paragraphStyle:(NSParagraphStyle *)paragraphStyle maxWidth:(CGFloat)maxWidth;#pragma mark -- 】產生屬性字串#pragma mark -- 【處理時間字串+ (NSString *)getCurrentDateString;+ (NSString *)getCurrentDateStringWithFormat:(NSString *)dateFormat;+ (NSString *)getDateStringWithTimeInterval:(NSTimeInterval)timeInterval;+ (NSString *)getDateStringWithTimeInterval:(NSTimeInterval)timeInterval dateFormat:(NSString *)dateFormat;+ (NSTimeInterval)getTimeIntervalWithDateString:(NSString *)dateString;+ (NSDateComponents *)getDateComponentsWithDateString:(NSString *)dateString;+ (NSDateComponents *)getDateComponentsWithTimeInterval:(NSTimeInterval)timeInterval;+ (NSString *)getContentPublishedTimeStringWithDateString:(NSString *)dateString;#pragma mark -- 】處理時間字串#pragma mark -- 【處理網路請求相關字串+ (NSString *)getSafeDecodeStringFromJsonValue:(NSString *)jsonValue;/** * 被解析的url字串格式為:xxxxxx?a=xxx&b=xxx * * @param urlString urlString description * * @return return value description */+ (NSDictionary *)getParametersDictionaryWithUrlString:(NSString *)urlString;/** * 被解析的url字串格式為:xxxxxx/realname_320x640.png(/jpg) * * @param urlString urlString description * * @return return value description */+ (CGSize)getImageOriginalSizeWithUrlString:(NSString *)urlString;+ (CGSize)getImageShowSizeWithUrlString:(NSString *)urlString maxWidth:(NSInteger)maxWidth;/** * 被解析的url字串格式為:xxxxxx/realname_320x640.png(/jpg) * * @param originalUrlString originalUrlString description * @param newWidth newWidth description * * @return 在原urlString後增加類似"?act=resize&x=320",用於伺服器裁剪尺寸 */+ (NSString *)getImageResizedUrlStringWithOriginalUrlString:(NSString *)originalUrlString newWidth:(NSInteger)newWidth;#pragma mark -- 】處理網路請求相關字串#pragma mark -- 其他功能方法/** * 擷取字串的位元組長度(一個漢字佔兩個位元組長度) * * @param string string description * * @return return value description */+ (NSInteger)getBytesLengthWithString:(NSString *)string;/** * 驗證手機號是否合理 * * @param phoneNum phoneNum description * * @return return value description */+ (BOOL)isValidatedMobliePhoneNum:(NSString *)phoneNum;+ (void)printAllCurrentSupportedFonts;+ (NSString *)getDeviceVersion;+ (NSString *)getAppShortVersion;+ (NSString *)getAppBundleVersion;
說明:
1.計算字串尺寸的方法,sizeWithFont系列方法已經被廢物,建議改為boundingRectWithSize方法;NSAttributedString也有boundingRectWithSize方法,如果已知屬性字串可以直接使用。請查看UIKit/NSStringDrawing.h中NSString和NSAttributedString的擴充方法
+ (CGSize)getStringSizeWith:(NSString *)string attributes:(NSDictionary *)attributes{ CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; return size;}
3.產生屬性字串的方法中,NSMutableParagraphStyle的對象用於設定段落屬性(行高、行間距、段落間距、對齊、書寫方向、首行縮排、行首縮排、行尾縮排),請查看UIKit/NSParagraphStyle.h
+ (NSAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color lineHeight:(CGFloat)lineHeight maxWidth:(CGFloat)maxWidth{ CGFloat perLineHeight = [StringHelper getStringSizeWith:@"內容" font:font].height; CGFloat lineSpacing = (lineHeight - perLineHeight)/2.5;//2.5是在實際應用中,調校的值 perLineHeight = lineHeight - lineSpacing; //設定文欄位落 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = perLineHeight; paragraphStyle.maximumLineHeight = perLineHeight; paragraphStyle.minimumLineHeight = perLineHeight; paragraphStyle.lineBreakMode = commonLineBreakMode; paragraphStyle.lineSpacing = lineSpacing;//行間距 paragraphStyle.paragraphSpacing = 0;//段間距 paragraphStyle.alignment = commonTextAlignment; return [self getAttributedStringWithString:string font:font color:color paragraphStyle:paragraphStyle maxWidth:maxWidth];}+ (NSAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color paragraphStyle:(NSParagraphStyle *)paragraphStyle maxWidth:(CGFloat)maxWidth{ NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:font forKey:NSFontAttributeName]; [dic setObject:color forKey:NSForegroundColorAttributeName]; [dic setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; NSAttributedString* attributedString; if (string == nil) { attributedString = [[NSAttributedString alloc] initWithString:@" " attributes:dic]; }else{ attributedString = [[NSAttributedString alloc] initWithString:string attributes:dic]; } return attributedString;}
4.NSFontAttributeName、NSForegroundColorAttributeName、NSParagraphStyleAttributeName為NSAttributedString常用屬性索引值,更多查看UIKit/NSAttributedString.h
5.轉換時間字串,需要記錄一下常用時間格式佔位字元:
#pragma mark -- 時間格式預留位置//G: 公元時代,例如AD公元//yy: 年的後2位//yyyy: 完整年//MM: 月,顯示為1-12,帶前置0//MMM: 月,顯示為英文月份簡寫,如 Jan//MMMM: 月,顯示為英文月份全稱,如 Janualy//dd: 日,2位元表示,如02//d: 日,1-2位顯示,如2,無前置0//EEE: 簡寫星期幾,如Sun//EEEE: 全寫星期幾,如Sunday//aa: 上下午,AM/PM//H: 時,24小時制,0-23//HH: 時,24小時制,帶前置0//h: 時,12小時制,無前置0//hh: 時,12小時制,帶前置0//m: 分,1-2位//mm: 分,2位,帶前置0//s: 秒,1-2位//ss: 秒,2位,帶前置0//S: 毫秒//Z: GMT(時區)
+ (NSString *)getCurrentDateString{ return [self getCurrentDateStringWithFormat:@"yyyy-MM-dd HH:mm:ss"];}+ (NSString *)getCurrentDateStringWithFormat:(NSString *)dateFormat{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:dateFormat]; NSDate *currentDate = [NSDate date]; NSString *currentDateString = [dateFormatter stringFromDate:currentDate]; return currentDateString;}
6.getDateComponentsWithDateString方法用於得到日期組成對象,實現代碼、測試代碼、輸入log如下:
+ (NSDateComponents *)getDateComponentsWithDateString:(NSString *)dateString{ NSTimeInterval timeInterval = [self getTimeIntervalWithDateString:dateString]; return [self getDateComponentsWithTimeInterval:timeInterval];}+ (NSDateComponents *)getDateComponentsWithTimeInterval:(NSTimeInterval)timeInterval{ NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSCalendarUnit unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSDateComponents *components = [calendar components:unitFlags fromDate:date]; return components;}
NSDateComponents *components = [StringHelper getDateComponentsWithDateString:@"2016-09-12 12:56:10"]; LOG(@"%@", components); components = [StringHelper getDateComponentsWithDateString:@"2016-09-11 12:56:10"]; LOG(@"%@", components); components = [StringHelper getDateComponentsWithDateString:@"2016-09-10 12:56:10"]; LOG(@"%@", components);
2016-09-12 13:02:12.283 base[14229:1700476] <NSDateComponents: 0x7fb0f9570770> Calendar Year: 2016 Month: 9 Leap month: no Day: 12 Hour: 12 Minute: 56 Second: 10 Week of Year: 38 Week of Month: 3 Weekday: 22016-09-12 13:02:15.600 base[14229:1700476] <NSDateComponents: 0x7fb0f961e760> Calendar Year: 2016 Month: 9 Leap month: no Day: 11 Hour: 12 Minute: 56 Second: 10 Week of Year: 38 Week of Month: 3 Weekday: 12016-09-12 13:02:15.601 base[14229:1700476] <NSDateComponents: 0x7fb0f94b6620> Calendar Year: 2016 Month: 9 Leap month: no Day: 10 Hour: 12 Minute: 56 Second: 10 Week of Year: 37 Week of Month: 2 Weekday: 7
需要注意的是,Weekday“一”為“周日”,“七”為“周六”,詳情可以查看《聖經 創世紀》;Week of Month表示本月第幾周;Week of Year表示今年第幾周。
7.getDeviceVersion方法更新了iPhone 7和iPhone 7 Plus的裝置版本判斷,詳情參考:https://www.theiphonewiki.com/wiki/Models
8.隨便一提,如果在新增的NSObject類中,無法使用CGFloat、CGPoint之類的類型,是因為沒有引用這些類型所在的標頭檔,在先行編譯標頭檔中引用即可:#import <UIKit/UIKit.h>
base項目已更新:git@github.com:ALongWay/base.git