Skip to content

Commit

Permalink
Update Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
changsanjiang committed Mar 21, 2018
1 parent ed8dd29 commit 2c095db
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 28 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [SJVideoListTableViewCell heightWithContentHeight:_videosM[indexPath.row].contentHeight];
return [SJVideoListTableViewCell heightWithContentHeight:_videosM[indexPath.row].videoContentLayout.textBoundingSize.height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [SJVideoListTableViewCell heightWithContentHeight:_videos[indexPath.row].contentHeight];
return [SJVideoListTableViewCell heightWithContentHeight:_videos[indexPath.row].videoContentLayout.textBoundingSize.height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <UIKit/UIKit.h>

@class SJUserModel;
@class SJUserModel, YYTextLayout;

@protocol NSAttributedStringTappedDelegate;

Expand All @@ -32,9 +32,9 @@


#pragma mark -
@property (nonatomic, strong, readonly) NSAttributedString *attributedTitle;
@property (nonatomic, assign, readonly) CGFloat contentHeight;
@property (nonatomic, strong, readonly) NSString *createTimeStr;
@property (nonatomic, strong, readonly) YYTextLayout *videoContentLayout;
@property (nonatomic, strong, readonly) YYTextLayout *nicknameLayout;
@property (nonatomic, strong, readonly) YYTextLayout *createTimeLayout;
@end

#pragma mark -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,26 @@ @implementation SJVideoModel
make.regexp(actionStrRexp, ^(SJAttributesRangeOperator * _Nonnull matched) {
matched.textColor([UIColor purpleColor]);
});

model -> _contentHeight = make.sizeByWidth(contentMaxWidth).height;
});
attrStr.addTapAction(actionStrRexp);
attrStr.tappedDelegate = actionDelegate;

model -> _attributedTitle = attrStr;
model -> _createTimeStr = sj_processTime(model.createTime, [NSDate date].timeIntervalSince1970); // this test time
model.videoContentLayout = sj_layout(contentMaxWidth, attrStr);
}];

[SJVideoListTableViewCell sync_makeNickName:^(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor) {
NSMutableAttributedString *attrStr = sj_makeAttributesString(^(SJAttributeWorker * _Nonnull make) {
make.font(font).textColor(textColor);
make.insert(model.creator.nickname, 0);
});
model.nicknameLayout = sj_layout(contentMaxWidth, attrStr);
}];

[SJVideoListTableViewCell sync_makeCreateTime:^(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor) {
NSMutableAttributedString *attrStr = sj_makeAttributesString(^(SJAttributeWorker * _Nonnull make) {
make.font(font).textColor(textColor);
make.insert(sj_processTime(model.createTime, [NSDate date].timeIntervalSince1970), 0) ; // this test time
});
model.createTimeLayout = sj_layout(contentMaxWidth, attrStr);
}];

[testVideosM addObject:model];
Expand All @@ -80,6 +92,24 @@ - (instancetype)initWithTitle:(NSString *)title videoId:(NSInteger)videoId creat
return self;
}

- (void)setVideoContentLayout:(YYTextLayout *)videoContentLayout {
_videoContentLayout = videoContentLayout;
}
- (void)setNicknameLayout:(YYTextLayout *)nicknameLayout {
_nicknameLayout = nicknameLayout;
}
- (void)setCreateTimeLayout:(YYTextLayout *)createTimeLayout {
_createTimeLayout = createTimeLayout;
}

static YYTextLayout *sj_layout(CGFloat contentMaxWidth, NSAttributedString *attrStr) {
YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(contentMaxWidth, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
YYTextLayout *layout = [YYTextLayout sj_layoutWithContainer:container text:attrStr];
return layout;
}

static NSString *sj_processTime(NSTimeInterval createDate, NSTimeInterval nowDate) {

double value = nowDate - createDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

+ (void)sync_makeVideoContent:(void(^)(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor))block;

+ (void)sync_makeNickName:(void (^)(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor))block;

+ (void)sync_makeCreateTime:(void (^)(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor))block;

// data source
@property (nonatomic, strong) SJVideoModel *model;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ + (void)sync_makeVideoContent:(void(^)(CGFloat contentMaxWidth, UIFont *font, UI
if ( block ) block([self contentMaxWidth], [UIFont boldSystemFontOfSize:14], [UIColor blackColor]);
}

+ (void)sync_makeNickName:(void (^)(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor))block {
if ( block ) block([self nicknameMaxWidth], [UIFont boldSystemFontOfSize:14], [UIColor blackColor]);
}

+ (void)sync_makeCreateTime:(void (^)(CGFloat contentMaxWidth, UIFont *font, UIColor *textColor))block {
if ( block ) block([self nicknameMaxWidth], [UIFont boldSystemFontOfSize:12], [UIColor blackColor]);
}


+ (CGFloat)heightWithContentHeight:(CGFloat)contentHeight {
return 14 + 44 + 8 + contentHeight + 8 + ([self contentMaxWidth] * 9 / 16.0f) + 8;
}
Expand Down Expand Up @@ -81,13 +90,12 @@ - (void)setModel:(SJVideoModel *)model {
_model = model;
_avatarImageView.image = [UIImage imageNamed:model.creator.avatar];
_coverImageView.image = [UIImage imageNamed:model.coverURLStr];
_nameLabel.text = model.creator.nickname;
_createTimeLabel.text = model.createTimeStr;
_contentLabel.attributedText = model.attributedTitle;
_nameLabel.textLayout = model.nicknameLayout;
_createTimeLabel.textLayout = model.createTimeLayout;
_contentLabel.textLayout = model.videoContentLayout;
}

- (void)_setupViews {

self.clipsToBounds = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.avatarImageView];
Expand Down Expand Up @@ -176,20 +184,23 @@ - (YYTapActionLabel *)contentLabel {
_contentLabel.numberOfLines = 0;
_contentLabel.userInteractionEnabled = YES;
_contentLabel.preferredMaxLayoutWidth = [[self class] contentMaxWidth];
_contentLabel.displaysAsynchronously = YES;
_contentLabel.ignoreCommonProperties = YES;
return _contentLabel;
}
- (YYLabel *)nameLabel {
if ( _nameLabel ) return _nameLabel;
_nameLabel = [YYLabel new];
_nameLabel.preferredMaxLayoutWidth = [[self class] nicknameMaxWidth];
_nameLabel.font = [UIFont boldSystemFontOfSize:14];
_nameLabel.displaysAsynchronously = YES;
_nameLabel.ignoreCommonProperties = YES;
return _nameLabel;
}
- (YYLabel *)createTimeLabel {
if ( _createTimeLabel ) return _createTimeLabel;
_createTimeLabel = [YYLabel new];
_createTimeLabel.font = [UIFont systemFontOfSize:12];
_createTimeLabel.textColor = [UIColor lightGrayColor];
_createTimeLabel.displaysAsynchronously = YES;
_createTimeLabel.ignoreCommonProperties = YES;
return _createTimeLabel;
}
- (UIView *)separatorLine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
#import "YYLabel.h"

NS_ASSUME_NONNULL_BEGIN
@protocol NSAttributedStringTappedDelegate;

@interface YYTapActionLabel : YYLabel

@end

@interface YYTextLayout(SJAdd)

+ (YYTextLayout *)sj_layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text;

@property (nonatomic, strong, readonly, nullable) NSAttributedString *tapActionAttributedString;

@end

#pragma mark -
@protocol NSAttributedStringTappedDelegate;

@interface NSAttributedString (SJAddDelegate)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,62 @@
#import <objc/message.h>

@interface YYTapActionLabel ()
@property (nonatomic, strong, readwrite, nullable) NSMutableAttributedString *tap_attributedText;
@property (nonatomic, weak, readwrite, nullable) id<NSAttributedStringTappedDelegate> tappedDelegate;
@end

@implementation YYTapActionLabel

- (void)setAttributedText:(NSAttributedString *)attributedText {
_tap_attributedText = nil;
_tappedDelegate = nil;
if ( attributedText.tappedDelegate && [attributedText isKindOfClass:[NSMutableAttributedString class]] ) {
_tap_attributedText = (NSMutableAttributedString *)attributedText;
_tappedDelegate = attributedText.tappedDelegate;
__weak typeof(self) _self = self;
self.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
__strong typeof(_self) self = _self;
if ( !self ) return;
if ( range.location >= text.length ) return;
if ( [text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL] ) {
if ( [self.tappedDelegate respondsToSelector:@selector(attributedString:tappedStr:)] ) {
[self.tappedDelegate attributedString:self.tap_attributedText tappedStr:[self.tap_attributedText attributedSubstringFromRange:range]];
if ( [attributedText.tappedDelegate respondsToSelector:@selector(attributedString:tappedStr:)] ) {
[attributedText.tappedDelegate attributedString:attributedText tappedStr:[attributedText attributedSubstringFromRange:range]];
}
}
};;
}
[super setAttributedText:attributedText];
}

- (void)setTextLayout:(YYTextLayout *)textLayout {
NSAttributedString *attributedText = textLayout.tapActionAttributedString;
if ( attributedText ) {
__weak typeof(self) _self = self;
self.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
__strong typeof(_self) self = _self;
if ( !self ) return;
if ( range.location >= text.length ) return;
if ( [text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL] ) {
if ( [attributedText.tappedDelegate respondsToSelector:@selector(attributedString:tappedStr:)] ) {
[attributedText.tappedDelegate attributedString:attributedText tappedStr:[attributedText attributedSubstringFromRange:range]];
}
}
};;
}
[super setTextLayout:textLayout];
}

@end

@implementation YYTextLayout(SJAdd)

+ (YYTextLayout *)sj_layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text {
YYTextLayout *layout = [self layoutWithContainer:container text:text];
if ( text.tappedDelegate && [text isKindOfClass:[NSMutableAttributedString class]] ) {
layout.tapActionAttributedString = text;
}
return layout;
}
- (void)setTapActionAttributedString:(NSAttributedString * _Nullable)tapActionAttributedString {
objc_setAssociatedObject(self, @selector(tapActionAttributedString), tapActionAttributedString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSAttributedString *)tapActionAttributedString {
return objc_getAssociatedObject(self, _cmd);
}
@end

@implementation NSMutableAttributedString (SJAdd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [SJVideoListTableViewCell heightWithContentHeight:_videosM[indexPath.row].contentHeight];
return [SJVideoListTableViewCell heightWithContentHeight:_videosM[indexPath.row].videoContentLayout.textBoundingSize.height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down

0 comments on commit 2c095db

Please sign in to comment.