Skip to content

Commit

Permalink
修复一个NSParagraphStyle的bug
Browse files Browse the repository at this point in the history
该bug曾经导致NSParagraphStyle相关的功能无法通过模板进行覆盖,只要在模板和text里同时定义了NSParagraphStyle相关的属性,模板里的NSParagraphStyle属性就会被覆盖
  • Loading branch information
Matt Cai committed Nov 1, 2018
1 parent 63d380e commit 01bc3e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 6 deletions.
20 changes: 20 additions & 0 deletions NudeIn/NUDParagraphStyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// NUDParagraphStyle.h
// textExample
//
// Created by Ruite Chen on 2018/11/1.
// Copyright © 2018 com.CAI. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NUDParagraphStyle : NSMutableParagraphStyle

@end

@interface NSMutableParagraphStyle (NUDParagraphStyle)

- (void)nud_mergeParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle;

@end

98 changes: 98 additions & 0 deletions NudeIn/NUDParagraphStyle.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// NUDParagraphStyle.m
// textExample
//
// Created by Ruite Chen on 2018/11/1.
// Copyright © 2018 com.CAI. All rights reserved.
//

#import "NUDParagraphStyle.h"
#import <objc/runtime.h>

@interface NUDParagraphStyle ()
- (void)addTag:(NSString *)key;
- (NSArray<NSString *> *)keyTags;
@end

@implementation NUDParagraphStyle
- (void)setLineSpacing:(CGFloat)lineSpacing {
[super setLineSpacing:lineSpacing];
[self addTag:@"lineSpacing"];
}
- (void)setParagraphSpacing:(CGFloat)paragraphSpacing {
[super setParagraphSpacing:paragraphSpacing];
[self addTag:@"paragraphSpacing"];
}
- (void)setParagraphSpacingBefore:(CGFloat)paragraphSpacingBefore {
[super setParagraphSpacingBefore:paragraphSpacingBefore];
[self addTag:@"paragraphSpacingBefore"];
}
- (void)setAlignment:(NSTextAlignment)alignment {
[super setAlignment:alignment];
[self addTag:@"alignment"];
}
- (void)setFirstLineHeadIndent:(CGFloat)firstLineHeadIndent {
[super setFirstLineHeadIndent:firstLineHeadIndent];
[self addTag:@"firstLineHeadIndent"];
}
- (void)setHeadIndent:(CGFloat)headIndent {
[super setHeadIndent:headIndent];
[self addTag:@"headIndent"];
}
- (void)setTailIndent:(CGFloat)tailIndent {
[super setTailIndent:tailIndent];
[self addTag:@"tailIndent"];
}
- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode {
[super setLineBreakMode:lineBreakMode];
[self addTag:@"lineBreakMode"];
}
- (void)setMaximumLineHeight:(CGFloat)maximumLineHeight {
[super setMaximumLineHeight:maximumLineHeight];
[self addTag:@"maximumLineHeight"];
}
- (void)setMinimumLineHeight:(CGFloat)minimumLineHeight {
[super setMinimumLineHeight:minimumLineHeight];
[self addTag:@"minimumLineHeight"];
}
- (void)setBaseWritingDirection:(NSWritingDirection)baseWritingDirection {
[super setBaseWritingDirection:baseWritingDirection];
[self addTag:@"baseWritingDirection"];
}
- (void)setLineHeightMultiple:(CGFloat)lineHeightMultiple {
[super setLineHeightMultiple:lineHeightMultiple];
[self addTag:@"lineHeightMultiple"];
}
- (void)setHyphenationFactor:(float)hyphenationFactor {
[super setHyphenationFactor:hyphenationFactor];
[self addTag:@"hyphenationFactor"];
}

- (void)addTag:(NSString *)key {
NSMutableArray<NSString *> *keys = objc_getAssociatedObject(self, "com.NudeIn.NUDParagraphStyle.keys");
if (!keys) {
keys = [NSMutableArray new];
objc_setAssociatedObject(self, "com.NudeIn.NUDParagraphStyle.keys", keys, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
[keys addObject:key];
}
- (NSArray<NSString *> *)keyTags {
NSArray<NSString *> *keys = objc_getAssociatedObject(self, "com.NudeIn.NUDParagraphStyle.keys");
return keys;
}
@end

@implementation NSMutableParagraphStyle (NUDParagraphStyle)

- (void)nud_mergeParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle {
if ([paragraphStyle isKindOfClass:[NUDParagraphStyle class]]) {
for (NSString *key in ((NUDParagraphStyle *)paragraphStyle).keyTags) {
[self setValue:[paragraphStyle valueForKey:key] forKey:key];
}
}else {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"NSMutableParagraphStyle is not a NUDParagraphStyle" userInfo:nil];
}
}

@end
30 changes: 24 additions & 6 deletions NudeIn/NUDText.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "NUDAction.h"
#import "NUDTextUpdate.h"
#import "UIImage+NUDPainter.h"
#import "NUDParagraphStyle.h"
#import <objc/runtime.h>

@interface NUDText ()
Expand Down Expand Up @@ -363,7 +364,7 @@ - (void)mergeComp:(NUDBase *)comp {
- (NSMutableParagraphStyle *)currentParagraphStyle {
NSMutableParagraphStyle *style = [self.attributes objectForKey:NSParagraphStyleAttributeName];
if (!style) {
style = [NSMutableParagraphStyle new];
style = [NUDParagraphStyle new];
[self.attributes setObject:style forKey:NSParagraphStyleAttributeName];
}
return style;
Expand Down Expand Up @@ -488,12 +489,7 @@ - (NSMutableParagraphStyle *)currentParagraphStyle {
if (template) {

NSMutableDictionary *tplAttrs = [template.tplAttributes mutableCopy];
[tplAttrs removeObjectsForKeys:[self.attributes allKeys]];

if ([tplAttrs objectForKey:NSLinkAttributeName]) {
self.link(template.tplLinkSelector.target,template.tplLinkSelector.action);
[tplAttrs removeObjectForKey:NSLinkAttributeName];
}
if (self.countOfLinefeed == NSUIntegerMax) {
self.countOfLinefeed = template.parasiticalObj.countOfLinefeed;
}
Expand All @@ -504,6 +500,20 @@ - (NSMutableParagraphStyle *)currentParagraphStyle {
self.selector = template.parasiticalObj.selector;
}

if ([tplAttrs objectForKey:NSParagraphStyleAttributeName] && [self.attributes objectForKey:NSParagraphStyleAttributeName]) {
NSMutableParagraphStyle *style = [tplAttrs objectForKey:NSParagraphStyleAttributeName];
[style nud_mergeParagraphStyle:[self.attributes objectForKey:NSParagraphStyleAttributeName]];
[self.attributes setObject:style forKey:NSParagraphStyleAttributeName];
[tplAttrs removeObjectForKey:NSParagraphStyleAttributeName];
}

[tplAttrs removeObjectsForKeys:[self.attributes allKeys]];

if ([tplAttrs objectForKey:NSLinkAttributeName]) {
self.link(template.tplLinkSelector.target,template.tplLinkSelector.action);
[tplAttrs removeObjectForKey:NSLinkAttributeName];
}

[self.attributes addEntriesFromDictionary:tplAttrs];

if (template.parasiticalObj.shadowTag) {
Expand Down Expand Up @@ -661,6 +671,12 @@ - (NSDictionary *)tplAttributes {
- (void)mergeTemplate:(id<NUDTemplate>)tpl {
if ([tpl isKindOfClass:[NUDTextTemplate class]]) {
NUDTextTemplate *template = tpl;
if ([self.parasiticalObj.attributes objectForKey:NSParagraphStyleAttributeName] &&
[template.parasiticalObj.attributes objectForKey:NSParagraphStyleAttributeName]) {
NSMutableParagraphStyle *style = [self.parasiticalObj.attributes objectForKey:NSParagraphStyleAttributeName];
[style nud_mergeParagraphStyle:[template.parasiticalObj.attributes objectForKey:NSParagraphStyleAttributeName]];
[template.parasiticalObj.attributes removeObjectForKey:NSParagraphStyleAttributeName];
}
[self.parasiticalObj.attributes addEntriesFromDictionary:template.parasiticalObj.attributes];
NUDShadowTag *newTag = [template.parasiticalObj.shadowTag copy];
[newTag mergeShadowTag:self.parasiticalObj.shadowTag];
Expand Down Expand Up @@ -719,3 +735,5 @@ - (NSShadow *)makeShadow {
return nil;
}
@end


6 changes: 6 additions & 0 deletions textExample/textExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
9208DDF5218AD11C00161DBF /* NUDParagraphStyle.m in Sources */ = {isa = PBXBuildFile; fileRef = 9208DDF4218AD11C00161DBF /* NUDParagraphStyle.m */; };
923F6302215C6CDF00259DE6 /* UIImage+NUDPainter.m in Sources */ = {isa = PBXBuildFile; fileRef = 923F6301215C6CDF00259DE6 /* UIImage+NUDPainter.m */; };
924B16B620D3AFF700D74A81 /* NUDTouchTracking.m in Sources */ = {isa = PBXBuildFile; fileRef = 924B16B520D3AFF700D74A81 /* NUDTouchTracking.m */; };
9601DF3820BA78E2004811F0 /* NUDText.m in Sources */ = {isa = PBXBuildFile; fileRef = 9601DF2F20BA78E2004811F0 /* NUDText.m */; };
Expand All @@ -25,6 +26,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
9208DDF3218AD11C00161DBF /* NUDParagraphStyle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = NUDParagraphStyle.h; path = ../../NudeIn/NUDParagraphStyle.h; sourceTree = "<group>"; };
9208DDF4218AD11C00161DBF /* NUDParagraphStyle.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = NUDParagraphStyle.m; path = ../../NudeIn/NUDParagraphStyle.m; sourceTree = "<group>"; };
922CB5372180151600E7DA57 /* NUDAttributeSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NUDAttributeSet.h; path = ../../NudeIn/NUDAttributeSet.h; sourceTree = "<group>"; };
923F6300215C6CDF00259DE6 /* UIImage+NUDPainter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "UIImage+NUDPainter.h"; path = "../../NudeIn/UIImage+NUDPainter.h"; sourceTree = "<group>"; };
923F6301215C6CDF00259DE6 /* UIImage+NUDPainter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "UIImage+NUDPainter.m"; path = "../../NudeIn/UIImage+NUDPainter.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +97,8 @@
9601DF4820BA8E42004811F0 /* NudeIn.h */,
923F6300215C6CDF00259DE6 /* UIImage+NUDPainter.h */,
923F6301215C6CDF00259DE6 /* UIImage+NUDPainter.m */,
9208DDF3218AD11C00161DBF /* NUDParagraphStyle.h */,
9208DDF4218AD11C00161DBF /* NUDParagraphStyle.m */,
);
name = NudeIn;
sourceTree = "<group>";
Expand Down Expand Up @@ -213,6 +218,7 @@
files = (
962F3F7220A96B16009AC22A /* ViewController.m in Sources */,
969B313D20CC248C006D1609 /* NUDTextUpdate.m in Sources */,
9208DDF5218AD11C00161DBF /* NUDParagraphStyle.m in Sources */,
9601DF3820BA78E2004811F0 /* NUDText.m in Sources */,
9601DF4A20BA8E43004811F0 /* NUDAction.m in Sources */,
9601DF3A20BA78E2004811F0 /* NUDTextMaker.m in Sources */,
Expand Down

0 comments on commit 01bc3e9

Please sign in to comment.