Character string processing tools and App tools in the app development process

Source: Internet
Author: User

Character string processing tools and App tools in the app development process

Record string processing is not a simple task.

NSString is a type that can be seen everywhere in the code, and it is also a wide range of objects for application and processing. Here, we only need to record and explain it in a regular way.

# Pragma mark -- [calculate the string size + (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 :( optional *) 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 :( NSStrin G *) string font :( UIFont *) font maxHeight :( CGFloat) maxHeight; # pragma mark -- calculate string size # pragma mark -- [generate attribute string + (NSAttributedString *) usage :( NSString *) string font :( UIFont *) font color :( UIColor *) color lineHeight :( CGFloat) lineHeight maxWidth :( CGFloat) maxWidth; + (NSAttributedString *) getAttributedStringWithString :( NSString *) string font :( UIFont *) font color :( UIColor *) color pa RagraphStyle :( NSParagraphStyle *) paragraphStyle maxWidth :( CGFloat) maxWidth; # pragma mark -- generate attribute string # pragma mark -- [processing time string + (NSString *) getCurrentDateString; + (NSString *) getCurrentDateStringWithFormat :( NSString *) dateFormat; + (NSString *) getDateStringWithTimeInterval :( NSTimeInterval) timeInterval; + (NSString *) interval :( NSTimeInterval) timeInterval dateFormat :( NSString *) dateF Ormat; + (NSTimeInterval) values :( NSString *) dateString; + (NSDateComponents *) getDateComponentsWithDateString :( NSString *) dateString; + (NSDateComponents *) values :( NSTimeInterval) timeInterval; + (NSString *) getContentPublishedTimeStringWithDateString :( NSString *) dateString; # pragma mark --] processing time string # pragma mark -- [processing network request strings + (NSString *) getSafeDecodeS TringFromJsonValue :( NSString *) jsonValue;/*** the parsed url string format is: xxxxxx? A = xxx & B = xxx ** @ param urlString description ** @ return value description */+ (NSDictionary *) getParametersDictionaryWithUrlString :( NSString *) urlString; /*** format of the parsed url string: xxxxxx/realname_320x640.png (/jpg) ** @ param urlString description ** @ return value description */+ (CGSize) getImageOriginalSizeWithUrlString :( NSString *) urlString; + (CGSize) getImageShowSizeWi ThUrlString :( NSString *) urlString maxWidth :( NSInteger) maxWidth;/*** the parsed url string format is: xxxxxx/realname_320x640.png (/jpg) ** @ param originalUrlString description * @ param newWidth description ** @ return adds a format similar "? Act = resize & x = 320 ", used for server crop size */+ (NSString *) getImageResizedUrlStringWithOriginalUrlString :( NSString *) originalUrlString newWidth :( NSInteger) newWidth; # pragma mark -- string related to processing network requests # pragma mark -- other functional methods/*** obtain the length of a string in bytes (one Chinese Character occupies two bytes) ** @ param string description ** @ return value description */+ (NSInteger) getBytesLengthWithString :( NSString *) string; /*** verify whether the mobile phone number is reasonable ** @ param phoneNum description ** @ return value description */+ (BOOL) isValidatedMobliePhoneNum :( NSString *) phoneNum; + (void) printAllCurrentSupportedFonts; + (NSString *) getDeviceVersion; + (NSString *) getAppShortVersion; + (NSString *) getAppBundleVersion;

 

Note:

1. The sizeWithFont series method has been used as a waste to calculate the string size. We recommend that you change it to the boundingRectWithSize method. The NSAttributedString method also has the boundingRectWithSize method, which can be used directly if the attribute string is. See the NSString and NSAttributedString extension methods in UIKit/NSStringDrawing. h.

 

+ (CGSize)getStringSizeWith:(NSString *)string attributes:(NSDictionary *)attributes{    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;        return size;}

 

3. in the method for generating attribute strings, The NSMutableParagraphStyle object is used to set the section attributes (line height, line spacing, paragraph spacing, alignment, writing direction, first line indent, first line indent, and last line indent ), check UIKit/NSParagraphStyle. h

+ (NSAttributedString *) getAttributedStringWithString :( NSString *) string font :( UIFont *) font color :( UIColor *) color lineHeight :( CGFloat) lineHeight maxWidth :( CGFloat) maxWidth {CGFloat perLineHeight = [StringHelper getStringSizeWith: @ "content" font: font]. height; CGFloat lineSpacing = (lineHeight-perLineHeight)/2.5; // the adjusted value is perLineHeight = lineHeight-lineSpacing in actual applications; // set the text section NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle. lineHeightMultiple = perLineHeight; paragraphStyle. maximumLineHeight = perLineHeight; paragraphStyle. minimumLineHeight = perLineHeight; paragraphStyle. lineBreakMode = commonLineBreakMode; paragraphStyle. lineSpacing = lineSpacing; // line spacing paragraphStyle. paragraphSpacing = 0; // segment spacing paragraphStyle. alignment = commonTextAlignment; return [self getAttributedStringWithString: string font: font color: color paragraphStyle: paragraphStyle maxWidth: maxWidth] ;}+ (NSAttributedString *) Restart :( NSString *) string font :( UIFont *) font color :( UIColor *) color paragraphStyle :( NSParagraphStyle *) paragraphStyle maxWidth :( CGFloat) maxWidth {NSMutableDictionary * dic = [random dictionary]; [dic setObject: font forKey: NSFontAttributeName]; [dic setObject: color forKey: NSForegroundColorAttributeName]; [dic setObject: paragraphStyle forKey: Secret]; NSAttributedString * attributedString; if (string = nil) {attributedString = [[NSAttributedString alloc] initWithString: @ "attributes: dic];} else {attributedString = [[NSAttributedString alloc] initWithString: string attributes: dic];} return attributedString ;}

 

4. NSFontAttributeName, NSForegroundColorAttributeName, and NSParagraphStyleAttributeName are common attribute key values of NSAttributedString. For more information, see UIKit/NSAttributedString. h.

5. to convert the time string, you need to record the placeholder characters in common time formats:

# Pragma mark -- Time Format placeholder // G: era, for example, AD // yy: the last two digits of the year // yyyy: full year // MM: month, display as 1-12, with a front 0 // MMM: Month, display as short as the English month, such as Jan // MMMM: Month, show as the full name of the English month, for example, Janualy // dd: Day, 2 digits, for example, 02 // d: Day, 1-2 digits, for example, 2, no front 0 // EEE: abbreviated to the day of the week, for example, Sun // EEEE: the day of the week, for example, Sunday // aa: Upper afternoon, AM/PM // H: hour, in the 24-hour format, 0-23 // HH: hour, 24-hour system, with a front 0 // h: hour, 12-hour system, without a front 0 // hh: hour, 12-hour system, with a front 0 // m: minute, 1-2 bits // mm: minute, 2 bits, with front 0 // s: Second, 1-2 bits // ss: Second, 2 bits, with front 0 // S: millisecond // Z: GMT (Time Zone)
+ (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. The getDateComponentsWithDateString method is used to obtain the date composition object. The implementation code, test code, and input log are as follows:

+ (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

It should be noted that Weekday "1" is "Sunday" and "7" is "Saturday". For details, refer to the Bible genesis; Week of Month indicates the Week of the Month; week of Year indicates the Week of the Year.

7. The getDeviceVersion method updates the iPhone 7 and iPhone 7 Plus device version judgment, see: https://www.theiphonewiki.com/wiki/Models

8. if the type CGFloat or CGPoint cannot be used in the newly added NSObject class because the header files of these types are not referenced, You can reference them in the pre-compiled header files: # import <UIKit/UIKit. h>

 

Base project Updated: git@github.com: ALongWay/base. git

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.