Skip to content

Commit

Permalink
Release 2.1.1
Browse files Browse the repository at this point in the history
- 增加图文对齐方式
- 修复设置字体间距错误的问题
  • Loading branch information
changsanjiang committed Jun 19, 2019
1 parent 628abc4 commit 7d92f2d
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 11 deletions.
Binary file modified Demo/.DS_Store
Binary file not shown.
4 changes: 0 additions & 4 deletions Demo/SJAttributesFactory.xcodeproj/project.pbxproj
Expand Up @@ -22,7 +22,6 @@
77D19C5C201DA87A00BE5412 /* SJDemoInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77D19C5B201DA87A00BE5412 /* SJDemoInfo.swift */; };
77E7E62F1FE794860061439B /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 77E7E62E1FE794860061439B /* README.md */; };
77E7E64D1FE795680061439B /* SJAttributesFactory.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 77E7E64C1FE795670061439B /* SJAttributesFactory.podspec */; };
77E7E6521FE7A3F60061439B /* action.gif in Resources */ = {isa = PBXBuildFile; fileRef = 77E7E6511FE7A3F60061439B /* action.gif */; };
A660298E226249C2006915E6 /* SJUTAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = A660297B226249C2006915E6 /* SJUTAttributes.m */; };
A660298F226249C2006915E6 /* SJUIKitTextMaker.m in Sources */ = {isa = PBXBuildFile; fileRef = A660297D226249C2006915E6 /* SJUIKitTextMaker.m */; };
A6602990226249C2006915E6 /* SJUTRangeHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = A660297E226249C2006915E6 /* SJUTRangeHandler.m */; };
Expand Down Expand Up @@ -57,7 +56,6 @@
77D19C5B201DA87A00BE5412 /* SJDemoInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SJDemoInfo.swift; sourceTree = "<group>"; };
77E7E62E1FE794860061439B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
77E7E64C1FE795670061439B /* SJAttributesFactory.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SJAttributesFactory.podspec; path = ../SJAttributesFactory.podspec; sourceTree = "<group>"; };
77E7E6511FE7A3F60061439B /* action.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = action.gif; sourceTree = "<group>"; };
7B5BBDDC8EBE6151143BEA0B /* Pods-SJAttributesFactory.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SJAttributesFactory.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SJAttributesFactory/Pods-SJAttributesFactory.debug.xcconfig"; sourceTree = "<group>"; };
A61066CC1FB0D08A00A67DF9 /* sample1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = sample1.png; sourceTree = "<group>"; };
A660297B226249C2006915E6 /* SJUTAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SJUTAttributes.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -160,7 +158,6 @@
77D19C57201DA6C600BE5412 /* Asset */ = {
isa = PBXGroup;
children = (
77E7E6511FE7A3F60061439B /* action.gif */,
A61066CC1FB0D08A00A67DF9 /* sample1.png */,
779A59E41FB33AB100F88DEC /* sample2.png */,
77D19C38201D74FB00BE5412 /* regular.jpeg */,
Expand Down Expand Up @@ -366,7 +363,6 @@
A69E5BFD1FB09BDC00EB65B5 /* Assets.xcassets in Resources */,
77A974BC201DD89800C32711 /* SJAttributesStringMaker.podspec in Resources */,
77D19C3B201D7E0800BE5412 /* common.jpeg in Resources */,
77E7E6521FE7A3F60061439B /* action.gif in Resources */,
77E7E62F1FE794860061439B /* README.md in Resources */,
A69E5BFB1FB09BDC00EB65B5 /* Main.storyboard in Resources */,
779A59E51FB33AB100F88DEC /* sample2.png in Resources */,
Expand Down
Binary file not shown.
24 changes: 24 additions & 0 deletions Demo/SJAttributesFactory/ViewController.m
Expand Up @@ -166,6 +166,30 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}];
}
break;
case 4: {
tips = @"图文对齐";
attr = [NSAttributedString sj_UIKitText:^(id<SJUIKitTextMakerProtocol> _Nonnull make) {
make.font([UIFont systemFontOfSize:14]); ///< 图文对齐, 需要Text为统一字体时才生效.
make.append(@"Top->");
make.appendImage(^(id<SJUTImageAttachment> _Nonnull make) {
make.image = [UIImage imageNamed:@"sample2"];
make.alignment = SJUTVerticalAlignmentTop;
});

make.append(@"Center->");
make.appendImage(^(id<SJUTImageAttachment> _Nonnull make) {
make.image = [UIImage imageNamed:@"sample2"];
make.alignment = SJUTVerticalAlignmentCenter;
});

make.append(@"Bottom->");
make.appendImage(^(id<SJUTImageAttachment> _Nonnull make) {
make.image = [UIImage imageNamed:@"sample2"];
make.alignment = SJUTVerticalAlignmentBottom;
});
}];
}
break;
// case 8: {
// tips = @"测试";
//
Expand Down
Binary file removed Demo/SJAttributesFactory/action.gif
Binary file not shown.
25 changes: 24 additions & 1 deletion Demo/SwiftVersion/ViewController.swift
Expand Up @@ -134,7 +134,30 @@ class ViewController: UIViewController {
model.task = { return text }
break;
default:
model.name = "\(i) 备用"
model.name = "\(i) 图文对齐"
let text = NSAttributedString.sj.makeText { (make) in
make.font(UIFont.systemFont(ofSize: 14))
make.append("Top->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .top
})

make.append("Center->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .center
})

make.append("Bottom->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .bottom
})
}

model.size = text.sj_textSize(forPreferredMaxLayoutWidth: self.view.bounds.size.width - 80)
model.task = { return text }
break;
}
demosM.append(model)
Expand Down
2 changes: 1 addition & 1 deletion SJAttributesFactory.podspec
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "SJAttributesFactory"
s.version = "2.1.0"
s.version = "2.1.1"
s.summary = "富文本编辑工厂."

# This description is used to generate tags and improve search results.
Expand Down
7 changes: 7 additions & 0 deletions SJAttributesFactory/UIKitText/SJUIKitAttributesDefines.h
Expand Up @@ -190,8 +190,15 @@ typedef id<SJUTAttributesProtocol>_Nonnull(^SJUTBaseLineOffsetAttribute)(double
@property (nonatomic) NSUnderlineStyle style;
@end

typedef enum : NSUInteger {
SJUTVerticalAlignmentBottom = 0,
SJUTVerticalAlignmentCenter = 1,
SJUTVerticalAlignmentTop = 2,
} SJUTVerticalAlignment;

@protocol SJUTImageAttachment <NSObject>
@property (nonatomic, strong, nullable) UIImage *image;
@property (nonatomic) SJUTVerticalAlignment alignment; ///< Text为统一的字体时生效
@property (nonatomic) CGRect bounds;
@end
NS_ASSUME_NONNULL_END
Expand Down
29 changes: 28 additions & 1 deletion SJAttributesFactory/UIKitText/SJUIKitTextMaker.m
Expand Up @@ -131,9 +131,12 @@ - (NSMutableAttributedString *_Nullable)_convertToUIKitTextForUTAttributes:(SJUT
current = [[NSMutableAttributedString alloc] initWithString:recorder->string];
}
else if ( recorder->attachment != nil ) {
SJUTVerticalAlignment alignment = recorder->attachment.alignment;
UIImage *image = recorder->attachment.image;
CGRect bounds = recorder->attachment.bounds;
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = recorder->attachment.image;
attachment.bounds = recorder->attachment.bounds;
attachment.bounds = [self _adjustVerticalOffsetOfImageAttachmentForBounds:bounds imageSize:image.size alignment:alignment commonFont:self.recorder->font];
current = [NSAttributedString attributedStringWithAttachment:attachment].mutableCopy;
}

Expand Down Expand Up @@ -307,5 +310,29 @@ - (NSDictionary *)_convertToUIKitTextAttributesForUTAttributesRecorder:(SJUTReco
if ( recorder->baseLineOffset != nil ) attrs[NSBaselineOffsetAttributeName] = recorder->baseLineOffset;
return attrs;
}

- (CGRect)_adjustVerticalOffsetOfImageAttachmentForBounds:(CGRect)bounds imageSize:(CGSize)imageSize alignment:(SJUTVerticalAlignment)alignment commonFont:(UIFont *)font {
switch ( alignment ) {
case SJUTVerticalAlignmentTop: {
if ( CGSizeEqualToSize(CGSizeZero, bounds.size) ) {
bounds.size = imageSize;
}
CGFloat offset = -(bounds.size.height - ABS(font.capHeight));
bounds.origin.y = offset;
}
break;
case SJUTVerticalAlignmentCenter: {
if ( CGSizeEqualToSize(CGSizeZero, bounds.size) ) {
bounds.size = imageSize;
}
CGFloat offset = -(bounds.size.height * 0.5 - ABS(font.descender));
bounds.origin.y = offset;
}
break;
case SJUTVerticalAlignmentBottom: { }
break;
}
return bounds;
}
@end
NS_ASSUME_NONNULL_END
6 changes: 5 additions & 1 deletion SJAttributesFactory/UIKitText/SJUTAttributes.m
Expand Up @@ -48,7 +48,11 @@ - (SJUTLineSpacingAttribute)lineSpacing {

- (SJUTKernAttribute)kern {
return ^id<SJUTAttributesProtocol>(CGFloat kern) {
self.recorder->lineSpacing = @(kern);
///
/// Thanks @donggelaile
/// https://github.com/changsanjiang/SJAttributesFactory/issues/9
///
self.recorder->kern = @(kern);
return self;
};
}
Expand Down
3 changes: 2 additions & 1 deletion SJAttributesFactory/UIKitText/SJUTRecorder.h
Expand Up @@ -23,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface SJUTImageAttachment : NSObject<SJUTImageAttachment>
@property (nonatomic, strong, nullable) UIImage *image;
@property (nonatomic) CGRect bounds;
@property (nonatomic) SJUTVerticalAlignment alignment;
@end

@interface SJUTReplace : NSObject
Expand All @@ -31,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
@end

@interface SJUTRecorder : NSObject {
@public
@package
UIFont *_Nullable font;
UIColor *_Nullable textColor;
UIColor *_Nullable backgroundColor;
Expand Down
2 changes: 1 addition & 1 deletion SJAttributesStringMaker.podspec
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "SJAttributesStringMaker"
s.version = "2.1.0"
s.version = "2.1.1"
s.summary = "富文本编辑工厂."

# This description is used to generate tags and improve search results.
Expand Down
30 changes: 29 additions & 1 deletion SJAttributesStringMaker/SJUIKitTextMaker.swift
Expand Up @@ -288,9 +288,16 @@ public class SJUTAttributes {
public var style: NSUnderlineStyle?
}

public enum SJUTVerticalAlignment {
case bottom
case center
case top
}

public struct SJUTImageAttachment {
public var image: UIImage?
public var bounds: CGRect?
public var alignment: SJUTVerticalAlignment = .bottom
}

fileprivate struct SJUTRecorder {
Expand Down Expand Up @@ -493,7 +500,7 @@ public class SJUIKitTextMaker: SJUTAttributes {
else if let attachment = ut.recorder.attachment {
let textAttachment = NSTextAttachment.init()
textAttachment.image = attachment.image
textAttachment.bounds = attachment.bounds ?? CGRect.zero
textAttachment.bounds = adjustVerticalOffsetOfImageAttachment(attachment.bounds ?? CGRect.zero, attachment.image?.size ?? CGSize.zero, attachment.alignment, self.recorder.font!)
current = NSAttributedString.init(attachment: textAttachment).mutableCopy() as? NSMutableAttributedString
}

Expand Down Expand Up @@ -610,6 +617,27 @@ public class SJUIKitTextMaker: SJUTAttributes {
return dict
}

private func adjustVerticalOffsetOfImageAttachment(_ bounds: CGRect, _ imageSize: CGSize, _ alignment: SJUTVerticalAlignment, _ commonFont: UIFont) -> CGRect {
var r_bounds = bounds;
switch alignment {
case .top:
if ( __CGSizeEqualToSize(CGSize.zero, bounds.size) ) {
r_bounds.size = imageSize;
}
let offset = -(r_bounds.size.height - abs(commonFont.capHeight))
r_bounds.origin.y = offset
case .center:
if ( __CGSizeEqualToSize(CGSize.zero, bounds.size) ) {
r_bounds.size = imageSize;
}
let offset = -(r_bounds.size.height * 0.5 - abs(commonFont.descender))
r_bounds.origin.y = offset;
case .bottom: break
}

return r_bounds
}

private func textRange(_ text: NSAttributedString)-> NSRange {
return NSRange.init(location: 0, length: text.length)
}
Expand Down

0 comments on commit 7d92f2d

Please sign in to comment.