diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h index 3a229bac..f578c1b5 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h @@ -32,10 +32,13 @@ #import + + @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; diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m index 83f8ee06..b725d1f5 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m @@ -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; } diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h index 9a454206..2b07cc30 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h @@ -46,6 +46,11 @@ typedef enum { SDCycleScrollViewPageContolStyleNone // 不显示pagecontrol } SDCycleScrollViewPageContolStyle; +typedef enum : NSUInteger { + SDDisplayTypeNormalText, // 显示正常文字 + SDDisplayTypeAttributeText, // 显示attribute文字 +} SDDisplayType; + @class SDCycleScrollView; @protocol SDCycleScrollViewDelegate @@ -140,6 +145,9 @@ typedef enum { /** 分页控件位置 */ @property (nonatomic, assign) SDCycleScrollViewPageContolAliment pageControlAliment; +/** 文字显示样式 */ +@property (nonatomic, assign) SDDisplayType displayType; + /** 分页控件距离轮播图的底部间距(在默认间距基础上)的偏移量 */ @property (nonatomic, assign) CGFloat pageControlBottomOffset; diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m index 2de2c230..e4b865b1 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m @@ -33,8 +33,8 @@ #import "SDCollectionViewCell.h" #import "UIView+SDExtension.h" #import "TAPageControl.h" -#import -#import +#import "UIImageView+WebCache.h" +#import "SDImageCache.h" #define kCycleScrollViewInitialPageControlDotSize CGSizeMake(10, 10) @@ -67,7 +67,6 @@ - (instancetype)initWithFrame:(CGRect)frame - (void)awakeFromNib { - [super awakeFromNib]; [self initialization]; [self setupMainView]; } @@ -92,6 +91,7 @@ - (void)initialization _currentPageDotColor = [UIColor whiteColor]; _pageDotColor = [UIColor lightGrayColor]; _bannerImageViewContentMode = UIViewContentModeScaleToFill; + _displayType = SDDisplayTypeNormalText; self.backgroundColor = [UIColor lightGrayColor]; @@ -453,7 +453,7 @@ - (void)clearCache + (void)clearImagesCache { - [[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil]; + [[[SDWebImageManager sharedManager] imageCache] clearDisk]; } #pragma mark - life circles @@ -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]; @@ -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; diff --git a/SDCycleScrollView/ViewController.m b/SDCycleScrollView/ViewController.m index cf842cbd..9e63e169 100644 --- a/SDCycleScrollView/ViewController.m +++ b/SDCycleScrollView/ViewController.m @@ -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 @@ -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