Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@

#import <UIKit/UIKit.h>



@interface SDCollectionViewCell : UICollectionViewCell

@property (weak, nonatomic) UIImageView *imageView;
@property (copy, nonatomic) NSString *title;
@property (nonatomic, copy) NSAttributedString *attributeTitle;

@property (nonatomic, strong) UIColor *titleLabelTextColor;
@property (nonatomic, strong) UIFont *titleLabelTextFont;
Expand Down
13 changes: 12 additions & 1 deletion SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,24 @@ - (void)setupTitleLabel
UILabel *titleLabel = [[UILabel alloc] init];
_titleLabel = titleLabel;
_titleLabel.hidden = YES;
_titleLabel.numberOfLines = 0;
[self.contentView addSubview:titleLabel];
}

- (void)setTitle:(NSString *)title
{
_title = [title copy];
_titleLabel.text = [NSString stringWithFormat:@" %@", title];
_titleLabel.text = [NSString stringWithFormat:@"%@", title];
if (_titleLabel.hidden) {
_titleLabel.hidden = NO;
}
}

- (void)setAttributeTitle:(NSAttributedString *)attributeTitle {
_attributeTitle = [attributeTitle copy];
if ([attributeTitle isKindOfClass:[NSAttributedString class]]) {
_titleLabel.attributedText = attributeTitle;
}
if (_titleLabel.hidden) {
_titleLabel.hidden = NO;
}
Expand Down
8 changes: 8 additions & 0 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ typedef enum {
SDCycleScrollViewPageContolStyleNone // 不显示pagecontrol
} SDCycleScrollViewPageContolStyle;

typedef enum : NSUInteger {
SDDisplayTypeNormalText, // 显示正常文字
SDDisplayTypeAttributeText, // 显示attribute文字
} SDDisplayType;

@class SDCycleScrollView;

@protocol SDCycleScrollViewDelegate <NSObject>
Expand Down Expand Up @@ -140,6 +145,9 @@ typedef enum {
/** 分页控件位置 */
@property (nonatomic, assign) SDCycleScrollViewPageContolAliment pageControlAliment;

/** 文字显示样式 */
@property (nonatomic, assign) SDDisplayType displayType;

/** 分页控件距离轮播图的底部间距(在默认间距基础上)的偏移量 */
@property (nonatomic, assign) CGFloat pageControlBottomOffset;

Expand Down
37 changes: 20 additions & 17 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#import "SDCollectionViewCell.h"
#import "UIView+SDExtension.h"
#import "TAPageControl.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"

#define kCycleScrollViewInitialPageControlDotSize CGSizeMake(10, 10)

Expand Down Expand Up @@ -67,7 +67,6 @@ - (instancetype)initWithFrame:(CGRect)frame

- (void)awakeFromNib
{
[super awakeFromNib];
[self initialization];
[self setupMainView];
}
Expand All @@ -92,6 +91,7 @@ - (void)initialization
_currentPageDotColor = [UIColor whiteColor];
_pageDotColor = [UIColor lightGrayColor];
_bannerImageViewContentMode = UIViewContentModeScaleToFill;
_displayType = SDDisplayTypeNormalText;

self.backgroundColor = [UIColor lightGrayColor];

Expand Down Expand Up @@ -453,7 +453,7 @@ - (void)clearCache

+ (void)clearImagesCache
{
[[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil];
[[[SDWebImageManager sharedManager] imageCache] clearDisk];
}

#pragma mark - life circles
Expand Down Expand Up @@ -543,6 +543,17 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
if (!cell.hasConfigured) {
cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
cell.titleLabelHeight = self.titleLabelHeight;
cell.titleLabelTextAlignment = self.titleLabelTextAlignment;
cell.titleLabelTextColor = self.titleLabelTextColor;
cell.titleLabelTextFont = self.titleLabelTextFont;
cell.hasConfigured = YES;
cell.imageView.contentMode = self.bannerImageViewContentMode;
cell.clipsToBounds = YES;
cell.onlyDisplayText = self.onlyDisplayText;
}
long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item];

NSString *imagePath = self.imagePathsGroup[itemIndex];
Expand All @@ -562,19 +573,11 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
}

if (_titlesGroup.count && itemIndex < _titlesGroup.count) {
cell.title = _titlesGroup[itemIndex];
}

if (!cell.hasConfigured) {
cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
cell.titleLabelHeight = self.titleLabelHeight;
cell.titleLabelTextAlignment = self.titleLabelTextAlignment;
cell.titleLabelTextColor = self.titleLabelTextColor;
cell.titleLabelTextFont = self.titleLabelTextFont;
cell.hasConfigured = YES;
cell.imageView.contentMode = self.bannerImageViewContentMode;
cell.clipsToBounds = YES;
cell.onlyDisplayText = self.onlyDisplayText;
if (self.displayType == SDDisplayTypeAttributeText) {
cell.attributeTitle = _titlesGroup[itemIndex];
}else {
cell.title = _titlesGroup[itemIndex];
}
}

return cell;
Expand Down
32 changes: 32 additions & 0 deletions SDCycleScrollView/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ - (void)viewDidLoad {

[demoContainerView addSubview:cycleScrollView4];

// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图5 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

// 网络加载 --- 创建只上下滚动展示文字的轮播器(文字支持attribute属性)
// 由于模拟器的渲染问题,如果发现轮播时有一条线不必处理,模拟器放大到100%或者真机调试是不会出现那条线的
SDCycleScrollView *cycleScrollView5 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 820, w, 55) delegate:self placeholderImage:nil];
cycleScrollView5.scrollDirection = UICollectionViewScrollDirectionVertical;
cycleScrollView5.onlyDisplayText = YES;
cycleScrollView5.displayType = SDDisplayTypeAttributeText;

NSMutableArray *attributeTitleArray = [NSMutableArray new];
[attributeTitleArray addObject:[self attributeStringWithContent:@"纯文字上下滚动轮播\n新增支持attribute属性。"]];
[attributeTitleArray addObject:[self attributeStringWithContent:@"纯文字上下滚动轮播\n新增支持attribute属性。"]];
[attributeTitleArray addObject:[self attributeStringWithContent:@"纯文字上下滚动轮播\n新增支持attribute属性。"]];
cycleScrollView5.titlesGroup = [attributeTitleArray copy];

[demoContainerView addSubview:cycleScrollView5];
}

- (void)viewWillAppear:(BOOL)animated
Expand Down Expand Up @@ -172,4 +188,20 @@ - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(N

*/

#pragma mark - SDCycleScrollViewDelegate
// 生成attribute字符串,测试使用
- (NSAttributedString *)attributeStringWithContent:(NSString *)content {
//创建NSMutableAttributedString
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:content];

//设置字体和设置字体的范围
[attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16.0f] range:NSMakeRange(0, 9)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:NSMakeRange(9, 16)];
//添加文字颜色
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 9)];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(9, 16)];

return attrStr;
}

@end