From dafc0e8d861c2990c4ff7ad3cafde540d6d705b2 Mon Sep 17 00:00:00 2001 From: Guangyao Jia Date: Sat, 28 Sep 2019 11:25:21 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=8C=E5=AF=B9NSMutableAttributedString=20?= =?UTF-8?q?=E7=9A=84api=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=98=B2=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E5=A4=84=E7=90=86=202=EF=BC=8C=E5=AF=B9NSString?= =?UTF-8?q?=E7=9A=84=20-=20(NSString=20*)stringByAppendingString:(NSString?= =?UTF-8?q?=20*)aString;=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=98=B2=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NSMutableAttributedString+AvoidCrash.h | 9 ++ .../NSMutableAttributedString+AvoidCrash.m | 151 ++++++++++++++++++ AvoidCrash/NSString+AvoidCrash.m | 24 +++ .../AvoidCrashDemo/ViewController.m | 120 +++++++++++++- README.md | 11 +- 5 files changed, 313 insertions(+), 2 deletions(-) diff --git a/AvoidCrash/NSMutableAttributedString+AvoidCrash.h b/AvoidCrash/NSMutableAttributedString+AvoidCrash.h index 91182c0..86ebff9 100644 --- a/AvoidCrash/NSMutableAttributedString+AvoidCrash.h +++ b/AvoidCrash/NSMutableAttributedString+AvoidCrash.h @@ -20,4 +20,13 @@ * * 1.- (instancetype)initWithString:(NSString *)str * 2.- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary *)attrs + * 3.- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range; + * 4.- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range; + * 5.- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range; + + * 6.- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; + * 7.- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc; + * 8.- (void)appendAttributedString:(NSAttributedString *)attrString; + * 9.- (void)deleteCharactersInRange:(NSRange)range; + * 10.- (void)setAttributedString:(NSAttributedString *)attrString; */ diff --git a/AvoidCrash/NSMutableAttributedString+AvoidCrash.m b/AvoidCrash/NSMutableAttributedString+AvoidCrash.m index dbfc103..2c1a998 100644 --- a/AvoidCrash/NSMutableAttributedString+AvoidCrash.m +++ b/AvoidCrash/NSMutableAttributedString+AvoidCrash.m @@ -24,6 +24,29 @@ + (void)avoidCrashExchangeMethod { //initWithString:attributes: [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(initWithString:attributes:) method2Sel:@selector(avoidCrashInitWithString:attributes:)]; + + //addAttribute:value:range: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(addAttribute:value:range:) method2Sel:@selector(avoidCrashAddAttribute:value:range:)]; + + //addAttributes:range: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(addAttributes:range:) method2Sel:@selector(avoidCrashAddAttributes:range:)]; + + //removeAttribute:range: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(removeAttribute:range:) method2Sel:@selector(avoidCrashRemoveAttributes:range:)]; + + //replaceCharactersInRange:withString: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(replaceCharactersInRange:withAttributedString:) method2Sel:@selector(avoidCrashReplaceCharactersInRange:withAttributedString:)]; + + //insertAttributedString:atIndex: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(insertAttributedString:atIndex:) method2Sel:@selector(avoidCrashInsertAttributedString:atIndex:)]; + + //appendString: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(appendString:) method2Sel:@selector(avoidCrashAppendAttributedString:)]; + + //setAttributedString: + [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(setAttributedString:) method2Sel:@selector(avoidCrashSetAttributedString:)]; + + }); } @@ -70,5 +93,133 @@ - (instancetype)avoidCrashInitWithString:(NSString *)str attributes:(NSDictionar } } +//================================================================= +// addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range; +//================================================================= +#pragma mark - addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range +- (void)avoidCrashAddAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range{ + @try { + [self avoidCrashAddAttribute:name value:value range:range]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + +//================================================================= +// addAttributes:(NSDictionary *)attrs range:(NSRange)range; +//================================================================= +#pragma mark - addAttributes:(NSDictionary *)attrs range:(NSRange)range +- (void)avoidCrashAddAttributes:(NSDictionary *)attrs range:(NSRange)range{ + @try{ + [self avoidCrashAddAttributes:attrs range:range]; + } + @catch (NSException *exception){ + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + +//================================================================= +// - (void)avoidCrashRemoveAttributes:(NSAttributedStringKey)name range:(NSRange)range; +//================================================================= +#pragma mark - removeAttributes:(NSAttributedStringKey)name range:(NSRange)range +- (void)avoidCrashRemoveAttributes:(NSAttributedStringKey)name range:(NSRange)range{ + @try { + [self avoidCrashRemoveAttributes:name range:range]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + +//================================================================= +// - (void)avoidCrashReplaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; +//================================================================= +#pragma mark - replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString +- (void)avoidCrashReplaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString{ + @try { + [self avoidCrashReplaceCharactersInRange:range withAttributedString:attrString]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + +//================================================================= +// - (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc; +//================================================================= +#pragma mark - insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc +- (void)avoidCrashInsertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc{ + @try { + [self avoidCrashInsertAttributedString:attrString atIndex:loc]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + + +//================================================================= +// - (void)appendAttributedString:(NSAttributedString *)attrString; +//================================================================= +#pragma mark - appendAttributedString:(NSAttributedString *)attrString +- (void)avoidCrashAppendAttributedString:(NSAttributedString *)attrString{ + @try { + [self avoidCrashAppendAttributedString:attrString]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + +//================================================================= +// - (void)deleteCharactersInRange:(NSRange)range; +//================================================================= +#pragma mark - deleteCharactersInRange:(NSRange)range +- (void)avoidCrashDeleteCharactersInRange:(NSRange)range{ + @try { + [self avoidCrashDeleteCharactersInRange:range]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} + + +//================================================================= +// - (void)setAttributedString:(NSAttributedString *)attrString; +//================================================================= +- (void)avoidCrashSetAttributedString:(NSAttributedString *)attrString{ + @try { + [self avoidCrashSetAttributedString:attrString]; + } + @catch (NSException *exception) { + [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore]; + } + @finally { + + } +} @end diff --git a/AvoidCrash/NSString+AvoidCrash.m b/AvoidCrash/NSString+AvoidCrash.m index bc52c49..aca7a59 100644 --- a/AvoidCrash/NSString+AvoidCrash.m +++ b/AvoidCrash/NSString+AvoidCrash.m @@ -38,6 +38,9 @@ + (void)avoidCrashExchangeMethod { //stringByReplacingCharactersInRange:withString: [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(stringByReplacingCharactersInRange:withString:) method2Sel:@selector(avoidCrashStringByReplacingCharactersInRange:withString:)]; + + //stringByAppendingString: + [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(stringByAppendingString:) method2Sel:@selector(avoidCrashStringByAppendingString:)]; }); } @@ -201,4 +204,25 @@ - (NSString *)avoidCrashStringByReplacingCharactersInRange:(NSRange)range withSt } +//================================================================= +// avoidCrashStringByAppendingString: +//================================================================= +#pragma mark - avoidCrashStringByAppendingString: + +- (NSString *)avoidCrashStringByAppendingString:(NSString *)aString{ + NSString *newStr = nil; + @try { + newStr = [self avoidCrashStringByAppendingString:aString]; + } + @catch (NSException *exception) { + NSString *defaultToDo = AvoidCrashDefaultReturnNil; + [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo]; + newStr = nil; + } + @finally { + return newStr; + } +} + + @end diff --git a/AvoidCrashDemo/AvoidCrashDemo/ViewController.m b/AvoidCrashDemo/AvoidCrashDemo/ViewController.m index ed111b8..54475cc 100644 --- a/AvoidCrashDemo/AvoidCrashDemo/ViewController.m +++ b/AvoidCrashDemo/AvoidCrashDemo/ViewController.m @@ -231,6 +231,12 @@ - (void)NSString_Test_stringByReplacingCharactersInRangeWithString { NSLog(@"%@",str); } +- (void)NSString_Test_stringByAppendingString { + NSString *str = @"TEST"; + NSString *nilString = nil; + str = [str stringByAppendingString:nilString]; + NSLog(@"%@",str); +} //================================================================= // NSMutableString_Test @@ -305,6 +311,110 @@ - (void)NSMutableAttributedString_Test_InitWithStringAttributes { NSLog(@"%@",attrStrM); } +- (void)NSMutableAttributedString_Test_AddAttributeValueRange{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSString *nilAtrrbutedName = nil; + [attrStrM addAttribute:nilAtrrbutedName value:[UIFont systemFontOfSize:12.0] range:NSMakeRange(0, [attrStrM.string length])]; + + UIFont *nilFont = nil; + [attrStrM addAttribute:NSFontAttributeName value:nilFont range:NSMakeRange(0, [attrStrM.string length])]; + + NSRange outOfRange = NSMakeRange(0, attrStrM.length + 100); + [attrStrM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:outOfRange]; + + [attrStrM addAttribute:nilAtrrbutedName value:nilFont range:outOfRange]; +} + +- (void)NSMutableAttributedString_Test_AddAttributesRange{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSDictionary *nilAttributes = nil; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + + [attrStrM addAttributes:nilAttributes range:NSMakeRange(0, [attrStrM.string length])]; + NSRange outOfRange = NSMakeRange(0, attrStrM.length + 100); + + [attrStrM addAttributes:attributes range:outOfRange]; + + [attrStrM addAttributes:nilAttributes range:outOfRange]; +} + +- (void)NSMutableAttributedString_Test_RemoveAttributesRange{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSString *nilAttributedName = nil; + [attrStrM removeAttribute:nilAttributedName range:NSMakeRange(0, attrStrM.length)]; + + NSRange outOfRange = NSMakeRange(0, attrStrM.length + 100); + [attrStrM removeAttribute:NSForegroundColorAttributeName range:outOfRange]; + + [attrStrM removeAttribute:nilAttributedName range:outOfRange]; +} + +- (void)NSMutableAttributedString_Test_ReplaceCharactersInRangeWithAttributedString{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSAttributedString *nilAttributedString = nil; + [attrStrM replaceCharactersInRange:NSMakeRange(0, attrStrM.length) withAttributedString:nilAttributedString]; + + NSRange outOfRange = NSMakeRange(0, attrStrM.length); + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"ABC" attributes:attributes]; + [attrStrM replaceCharactersInRange:outOfRange withAttributedString:attributedString]; + + [attrStrM replaceCharactersInRange:outOfRange withAttributedString:nilAttributedString]; +} + +- (void)NSMutableAttributedString_Test_InsertAttributedStringAtIndex{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSAttributedString *nilAttributedString = nil; + [attrStrM insertAttributedString:nilAttributedString atIndex:0]; + + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"ABC" attributes:attributes]; + NSInteger outIndex = attrStrM.length + 100; + [attrStrM insertAttributedString:attributedString atIndex:outIndex]; + + [attrStrM insertAttributedString:nilAttributedString atIndex:outIndex]; +} + +- (void)NSMutableAttributedString_Test_AppendAttributedString{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + + NSAttributedString *nilAttributedString = nil; + [attrStrM appendAttributedString:nilAttributedString]; +} + +- (void)NSMutableAttributedString_Test_DeleteCharactersInRange{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSRange outOfRange = NSMakeRange(0, attrStrM.length); + + [attrStrM deleteCharactersInRange:outOfRange]; +} + +- (void)NSMutableAttributedString_Test_SetAttributedString{ + NSDictionary *attributes = @{ + NSForegroundColorAttributeName : [UIColor redColor] + }; + NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:@"TEST" attributes:attributes]; + NSAttributedString *nilAttributedString = nil; + [attrStrM setAttributedString:nilAttributedString]; +} //================================================================= @@ -388,7 +498,7 @@ - (void)executeAllTestMethod { [self NSString_Test_StringByReplacingOccurrencesOfString]; [self NSString_Test_StringByReplacingOccurrencesOfStringRange]; [self NSString_Test_stringByReplacingCharactersInRangeWithString]; - + [self NSString_Test_stringByAppendingString]; [self NSMutableString_Test_ReplaceCharactersInRange]; [self NSMutableString_Test_InsertStringAtIndex]; @@ -402,6 +512,14 @@ - (void)executeAllTestMethod { [self NSMutableAttributedString_Test_InitWithString]; [self NSMutableAttributedString_Test_InitWithStringAttributes]; + [self NSMutableAttributedString_Test_AddAttributeValueRange]; + [self NSMutableAttributedString_Test_AddAttributesRange]; + [self NSMutableAttributedString_Test_RemoveAttributesRange]; + [self NSMutableAttributedString_Test_ReplaceCharactersInRangeWithAttributedString]; + [self NSMutableAttributedString_Test_InsertAttributedStringAtIndex]; + [self NSMutableAttributedString_Test_AppendAttributedString]; + [self NSMutableAttributedString_Test_DeleteCharactersInRange]; + [self NSMutableAttributedString_Test_SetAttributedString]; [self KVC_Test_SetValueForKey]; diff --git a/README.md b/README.md index ab15844..a84e58c 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ pod 'AvoidCrash', '~> 2.5.2' - `5. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement` - `6. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange` - `7. - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement` - + - `8. - (NSString *)stringByAppendingString:(NSString *)aString; --- @@ -280,6 +280,15 @@ pod 'AvoidCrash', '~> 2.5.2' - NSMutableAttributedString - `1.- (instancetype)initWithString:(NSString *)str` - `2.- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary *)attrs` + - 3.- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range; + - 4.- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range; + - 5.- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range; + - 6.- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; + - 7.- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc; + - 8.- (void)appendAttributedString:(NSAttributedString *)attrString; + - 9.- (void)deleteCharactersInRange:(NSRange)range; + - 10.- (void)setAttributedString:(NSAttributedString *)attrString; + 更新