From ffb6c53c76f834621f4d93f65ef715ee4af90172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BA=94=E5=B9=B3?= <1417alk@gmail.com> Date: Mon, 5 Dec 2016 19:01:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=82=E9=85=8D=20attr?= =?UTF-8?q?ibutedText?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TXScrollLabelView/TXScrollLabelView.h | 3 ++ TXScrollLabelView/TXScrollLabelView.m | 7 ++++ .../project.pbxproj | 6 +++ .../NSString+AttributedString.h | 16 ++++++++ .../NSString+AttributedString.m | 37 +++++++++++++++++++ .../TXScrollLabelViewDemo/ViewController.m | 6 +++ 6 files changed, 75 insertions(+) create mode 100644 TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.h create mode 100644 TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.m diff --git a/TXScrollLabelView/TXScrollLabelView.h b/TXScrollLabelView/TXScrollLabelView.h index 98f34ed..6332ac9 100644 --- a/TXScrollLabelView/TXScrollLabelView.h +++ b/TXScrollLabelView/TXScrollLabelView.h @@ -63,6 +63,9 @@ typedef NS_ENUM(NSInteger, TXScrollLabelViewType) { //根据内容自适应宽度 Pending!! @property (assign, nonatomic) BOOL autoWidth; +#pragma mark - setupAttributeTitle + +- (void)setupAttributeTitle:(NSAttributedString *)attributeTitle; #pragma mark - Class Methods diff --git a/TXScrollLabelView/TXScrollLabelView.m b/TXScrollLabelView/TXScrollLabelView.m index d7b12bb..b25af40 100644 --- a/TXScrollLabelView/TXScrollLabelView.m +++ b/TXScrollLabelView/TXScrollLabelView.m @@ -318,6 +318,13 @@ - (void)setupTitle:(NSString *)title { self.downLabel.text = title; } +- (void)setupAttributeTitle:(NSAttributedString *)attributeTitle { + _scrollTitle = attributeTitle.string; + [self setupSubviewsLayout]; + self.upLabel.attributedText = attributeTitle; + self.downLabel.attributedText = attributeTitle; +} + - (void)setupRepeatTypeLayout { CGFloat labelW = self.tx_width - _scrollInset.left - _scrollInset.right; CGFloat labelX = _scrollInset.left; diff --git a/TXScrollLabelViewDemo/TXScrollLabelViewDemo.xcodeproj/project.pbxproj b/TXScrollLabelViewDemo/TXScrollLabelViewDemo.xcodeproj/project.pbxproj index b0e3931..ebe6bda 100644 --- a/TXScrollLabelViewDemo/TXScrollLabelViewDemo.xcodeproj/project.pbxproj +++ b/TXScrollLabelViewDemo/TXScrollLabelViewDemo.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 97886DA71DB8E0AF00A0B326 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97886DA51DB8E0AF00A0B326 /* LaunchScreen.storyboard */; }; 97886DB21DB8E0AF00A0B326 /* TXScrollLabelViewDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 97886DB11DB8E0AF00A0B326 /* TXScrollLabelViewDemoTests.m */; }; 97886DBD1DB8E0AF00A0B326 /* TXScrollLabelViewDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 97886DBC1DB8E0AF00A0B326 /* TXScrollLabelViewDemoUITests.m */; }; + AF1B25351DF5803200AB8923 /* NSString+AttributedString.m in Sources */ = {isa = PBXBuildFile; fileRef = AF1B25341DF5803200AB8923 /* NSString+AttributedString.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -55,6 +56,8 @@ 97886DB81DB8E0AF00A0B326 /* TXScrollLabelViewDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TXScrollLabelViewDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 97886DBC1DB8E0AF00A0B326 /* TXScrollLabelViewDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TXScrollLabelViewDemoUITests.m; sourceTree = ""; }; 97886DBE1DB8E0AF00A0B326 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + AF1B25331DF5803200AB8923 /* NSString+AttributedString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+AttributedString.h"; sourceTree = ""; }; + AF1B25341DF5803200AB8923 /* NSString+AttributedString.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+AttributedString.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -122,6 +125,8 @@ 97886D9B1DB8E0AF00A0B326 /* AppDelegate.m */, 97886D9D1DB8E0AF00A0B326 /* ViewController.h */, 97886D9E1DB8E0AF00A0B326 /* ViewController.m */, + AF1B25331DF5803200AB8923 /* NSString+AttributedString.h */, + AF1B25341DF5803200AB8923 /* NSString+AttributedString.m */, 97886DA31DB8E0AF00A0B326 /* Assets.xcassets */, 97886DA51DB8E0AF00A0B326 /* LaunchScreen.storyboard */, 97886DA81DB8E0AF00A0B326 /* Info.plist */, @@ -295,6 +300,7 @@ 972620CA1DDE2F0600009F23 /* TXScrollLabelView.m in Sources */, 97886D9F1DB8E0AF00A0B326 /* ViewController.m in Sources */, 97886D9C1DB8E0AF00A0B326 /* AppDelegate.m in Sources */, + AF1B25351DF5803200AB8923 /* NSString+AttributedString.m in Sources */, 97886D991DB8E0AF00A0B326 /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.h b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.h new file mode 100644 index 0000000..a7e59cf --- /dev/null +++ b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.h @@ -0,0 +1,16 @@ +// +// NSString+AttributedString.h +// TXScrollLabelViewDemo +// +// Created by 陈应平 on 2016/12/5. +// Copyright © 2016年 tingxins. All rights reserved. +// + +#import + +@interface NSString (AttributedString) + +// 传递一个字符串数组,可以渲染不同的颜色,后期有需要可以把 color 和 font 也以数组的形式传递,配对使用 +- (NSMutableAttributedString *)setAttributedWithIdentifyStringArray:(NSArray *)identifyStringArray color:(UIColor *)color font:(UIFont *)font; + +@end diff --git a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.m b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.m new file mode 100644 index 0000000..2c6f8c1 --- /dev/null +++ b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/NSString+AttributedString.m @@ -0,0 +1,37 @@ +// +// NSString+AttributedString.m +// TXScrollLabelViewDemo +// +// Created by 陈应平 on 2016/12/5. +// Copyright © 2016年 tingxins. All rights reserved. +// + +#import "NSString+AttributedString.h" + +@implementation NSString (AttributedString) + +- (NSMutableAttributedString *)setAttributedWithIdentifyStringArray:(NSArray *)identifyStringArray color:(UIColor *)color font:(UIFont *)font +{ + if (!self && !identifyStringArray) { + return nil; + } + + if (!identifyStringArray.count) { + return nil; + } + + NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:self]; + for (NSString *identifyString in identifyStringArray) { + NSRange range = [self rangeOfString:identifyString]; + if (font) { + [attributedStr addAttribute:NSFontAttributeName value:font range:range]; + } + if (color) { + [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range]; + } + } + + return attributedStr; +} + +@end diff --git a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m index 6d682d9..453e26a 100644 --- a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m +++ b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m @@ -8,6 +8,7 @@ #import "ViewController.h" #import "TXScrollLabelView.h" +#import "NSString+AttributedString.h" @interface ViewController () @@ -55,6 +56,11 @@ - (void)setSubviews { //开始滚动 [scrollLabelView beginScrolling]; self.scrollLabelView = scrollLabelView; + + if (options == 0) { + NSMutableAttributedString *tempString = [@"2.1.1:本人使用了九宫格布局,为了方便大家,我给大家提供一个链接地址:(暂时还未整理好,需要的联系我qq154158462 请先关注这个博客哦,不然不通过哟),这个就是直接上面的UI效果,很方便的。" setAttributedWithIdentifyStringArray:@[@"我",@"使用了",@"给",@"UI",@"方便",@"链接",@"qq"] color:[UIColor redColor] font:nil]; + [scrollLabelView setupAttributeTitle:tempString]; + } } } From 1e340b69c6c31a670be80596b59ad712c684213f Mon Sep 17 00:00:00 2001 From: tingxins Date: Tue, 6 Dec 2016 00:10:30 +0800 Subject: [PATCH 2/2] Merge branch 'master' into pr/7 # Conflicts: # TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m --- TXScrollLabelView/TXScrollLabelView.h | 3 +- TXScrollLabelView/TXScrollLabelView.m | 71 +++++++++++++++---- TXScrollLabelView/UIView+TXFrame.h | 1 + .../TXScrollLabelViewDemo/ViewController.m | 69 ++++++++++-------- 4 files changed, 99 insertions(+), 45 deletions(-) diff --git a/TXScrollLabelView/TXScrollLabelView.h b/TXScrollLabelView/TXScrollLabelView.h index 6332ac9..e593d2f 100644 --- a/TXScrollLabelView/TXScrollLabelView.h +++ b/TXScrollLabelView/TXScrollLabelView.h @@ -3,6 +3,7 @@ // // Created by tingxins on 2/23/16. // Copyright © 2016 tingxins. All rights reserved. +// Welcome to my blog: https://tingxins.com // 滚动视图 #define TX_DEPRECATED_METHODS(explain) __attribute__((deprecated(explain))) @@ -48,7 +49,7 @@ typedef NS_ENUM(NSInteger, TXScrollLabelViewType) { @property (copy, nonatomic) NSString *scrollTitle; /** 滚动类型 */ @property (assign, nonatomic) TXScrollLabelViewType scrollType; -/** 滚动速率([1, 10]),单位秒s */ +/** 滚动速率([0, 10]),单位秒s,建议在初始化方法中设置该属性*/ @property (assign, nonatomic) NSTimeInterval scrollVelocity; /** 文本颜色 */ @property (strong, nonatomic) UIColor *scrollTitleColor; diff --git a/TXScrollLabelView/TXScrollLabelView.m b/TXScrollLabelView/TXScrollLabelView.m index b25af40..06a648d 100644 --- a/TXScrollLabelView/TXScrollLabelView.m +++ b/TXScrollLabelView/TXScrollLabelView.m @@ -166,10 +166,10 @@ - (instancetype)initWithTitle:(NSString *)scrollTitle options:(UIViewAnimationOptions)options inset:(UIEdgeInsets)inset { if (self = [super init]) { - self.scrollTitle = scrollTitle; - self.scrollType = scrollType; + _scrollTitle = scrollTitle; + _scrollType = scrollType; self.scrollVelocity = scrollVelocity; - self.options = options; + _options = options; _scrollInset = inset; } return self; @@ -225,7 +225,20 @@ + (instancetype)scrollWithTitle:(NSString *)scrollTitle } #pragma mark - Deprecated Getter & Setter Methods -/** WILL BE REMOVED IN FUTURE */ +/*************WILL BE REMOVED IN FUTURE.****************************/ + +- (void)setTx_scrollTitle:(NSString *)tx_scrollTitle { + self.scrollTitle = tx_scrollTitle; +} + +- (void)setTx_scrollType:(TXScrollLabelViewType)tx_scrollType { + self.scrollType = tx_scrollType; +} + +- (void)setTx_scrollVelocity:(NSTimeInterval)tx_scrollVelocity { + self.scrollVelocity = tx_scrollVelocity; +} + - (void)setTx_scrollContentSize:(CGRect)tx_scrollContentSize{ _tx_scrollContentSize = tx_scrollContentSize; self.frame = _tx_scrollContentSize; @@ -234,14 +247,29 @@ - (void)setTx_scrollContentSize:(CGRect)tx_scrollContentSize{ - (void)setTx_scrollTitleColor:(UIColor *)tx_scrollTitleColor { self.scrollTitleColor = tx_scrollTitleColor; } +/*************ALL ABOVE.*******************************************/ #pragma mark - Getter & Setter Methods +- (void)setScrollTitle:(NSString *)scrollTitle { + _scrollTitle = scrollTitle; +// self.scrollArray = nil; + [self resetScrollLabelView]; +} + +- (void)setScrollType:(TXScrollLabelViewType)scrollType { + if (_scrollType == scrollType) return; + + _scrollType = scrollType; + self.scrollVelocity = _scrollVelocity; + [self resetScrollLabelView]; +} + - (void)setScrollVelocity:(NSTimeInterval)scrollVelocity { CGFloat velocity = scrollVelocity; - if (scrollVelocity < 1) { - velocity = 1; + if (scrollVelocity < 0.1) { + velocity = 0.1; }else if (scrollVelocity > 10) { velocity = 10; } @@ -251,7 +279,11 @@ - (void)setScrollVelocity:(NSTimeInterval)scrollVelocity { }else { _scrollVelocity = velocity; } - +} + +- (UIViewAnimationOptions)options { + if (_options) return _options; + return _options = UIViewAnimationOptionCurveEaseInOut; } - (void)setScrollTitleColor:(UIColor *)scrollTitleColor { @@ -274,15 +306,11 @@ - (CGFloat)scrollSpace { return 0.f; } -- (UIViewAnimationOptions)options { - if (_options) return _options; - return _options = UIViewAnimationOptionCurveEaseInOut; -} - - (NSArray *)scrollArray { if (_scrollArray) return _scrollArray; return _scrollArray = [self getSeparatedLinesFromLabel]; } + - (void)setFrame:(CGRect)frame { [super setFrame:frame]; [self setupSubviewsLayout]; @@ -307,6 +335,12 @@ - (UIFont *)font { } #pragma mark - Custom Methods +/** 重置滚动视图 */ +- (void)resetScrollLabelView { + [self endScrolling];//停止滚动 + [self setupSubviewsLayout];//重新布局 + [self beginScrolling];//开始滚动 +} - (void)setupTextColor:(UIColor *)color { self.upLabel.textColor = color; @@ -391,6 +425,8 @@ - (void)setupSubviewsLayout { #pragma mark - Scrolling Operation Methods - (void)beginScrolling { + if (!self.scrollTitle.length) return; + [self endScrolling]; if (_scrollType == TXScrollLabelViewTypeFlipRepeat || _scrollType == TXScrollLabelViewTypeFlipNoRepeat) { @@ -407,15 +443,17 @@ - (void)beginScrolling { - (void)endScrolling { [self.scrollTimer invalidate]; self.scrollTimer = nil; + self.scrollArray = nil; } - (void)pauseScrolling { - [self.scrollTimer invalidate]; - self.scrollTimer = nil; + [self endScrolling]; } //开始计时 - (void)startWithVelocity:(NSTimeInterval)velocity { + if (!self.scrollTitle.length) return; + __weak typeof(self) weakSelf = self; self.scrollTimer = [NSTimer tx_scheduledTimerWithTimeInterval:velocity repeat:YES block:^(NSTimer *timer) { TXScrollLabelView *strongSelf = weakSelf; @@ -499,10 +537,13 @@ - (void)flipAnimationWithDelay:(NSTimeInterval)delay { } - (void)flipNoCleAnimationWithDelay:(NSTimeInterval)delay { + if (!self.scrollArray.count) return; + static int count = 0; + if (count >= self.scrollArray.count) count = 0; self.upLabel.text = self.scrollArray[count]; count ++; - if (count == self.scrollArray.count) count = 0; + if (count >= self.scrollArray.count) count = 0; self.downLabel.text = self.scrollArray[count]; [self flipAnimationWithDelay:delay]; } diff --git a/TXScrollLabelView/UIView+TXFrame.h b/TXScrollLabelView/UIView+TXFrame.h index 4b60622..6d32f42 100644 --- a/TXScrollLabelView/UIView+TXFrame.h +++ b/TXScrollLabelView/UIView+TXFrame.h @@ -4,6 +4,7 @@ // // Created by tingxins on 9/1/16. // Copyright © 2016 tingxins. All rights reserved. +// Welcome to my blog: https://tingxins.com // #import diff --git a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m index 453e26a..9f2ba1e 100644 --- a/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m +++ b/TXScrollLabelViewDemo/TXScrollLabelViewDemo/ViewController.m @@ -8,9 +8,8 @@ #import "ViewController.h" #import "TXScrollLabelView.h" -#import "NSString+AttributedString.h" -@interface ViewController () +@interface ViewController () @property (weak, nonatomic) TXScrollLabelView *scrollLabelView; @@ -35,34 +34,46 @@ - (void)setViewInfos { - (void)setSubviews { - for (int options = 0; options < 4; ++ options) { - NSString *scrollTitle = @"If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers"; - //options 是 TXScrollLabelViewType 枚举类型, 此处仅为了方便举例 - TXScrollLabelView *scrollLabelView = [TXScrollLabelView scrollWithTitle:scrollTitle type:options velocity:3 options:UIViewAnimationOptionTransitionFlipFromTop]; - [self.view addSubview:scrollLabelView]; - - //布局(Required) - scrollLabelView.frame = CGRectMake(50, 100 * (options + 0.7), 300, 30); - - //偏好(Optional) - scrollLabelView.tx_centerX = [UIScreen mainScreen].bounds.size.width * 0.5; - scrollLabelView.scrollInset = UIEdgeInsetsMake(0, 10 , 0, 10); - scrollLabelView.scrollSpace = 10; - scrollLabelView.font = [UIFont systemFontOfSize:15]; - scrollLabelView.textAlignment = NSTextAlignmentCenter; - scrollLabelView.backgroundColor = [UIColor blackColor]; - scrollLabelView.layer.cornerRadius = 5; - - //开始滚动 - [scrollLabelView beginScrolling]; - self.scrollLabelView = scrollLabelView; - - if (options == 0) { - NSMutableAttributedString *tempString = [@"2.1.1:本人使用了九宫格布局,为了方便大家,我给大家提供一个链接地址:(暂时还未整理好,需要的联系我qq154158462 请先关注这个博客哦,不然不通过哟),这个就是直接上面的UI效果,很方便的。" setAttributedWithIdentifyStringArray:@[@"我",@"使用了",@"给",@"UI",@"方便",@"链接",@"qq"] color:[UIColor redColor] font:nil]; - [scrollLabelView setupAttributeTitle:tempString]; - } - } + [self addWith:TXScrollLabelViewTypeLeftRight velocity:0.4]; + + [self addWith:TXScrollLabelViewTypeUpDown velocity:1]; + + [self addWith:TXScrollLabelViewTypeFlipRepeat velocity:2]; + + [self addWith:TXScrollLabelViewTypeFlipNoRepeat velocity:2]; } +- (void)addWith:(TXScrollLabelViewType)type velocity:(CGFloat)velocity { + /** Step1: 滚动文字 */ + NSString *scrollTitle = @"If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers"; + + /** Step2: 创建 ScrollLabelView */ + TXScrollLabelView *scrollLabelView = [TXScrollLabelView scrollWithTitle:scrollTitle type:type velocity:velocity options:UIViewAnimationOptionCurveEaseInOut]; + + /** Step3: 设置代理进行回调 */ + scrollLabelView.scrollLabelViewDelegate = self; + + /** Step4: 布局(Required) */ + scrollLabelView.frame = CGRectMake(50, 100 * (type + 0.7), 300, 30); + + [self.view addSubview:scrollLabelView]; + + //偏好(Optional), Preference,if you want. + scrollLabelView.tx_centerX = [UIScreen mainScreen].bounds.size.width * 0.5; + scrollLabelView.scrollInset = UIEdgeInsetsMake(0, 10 , 0, 10); + scrollLabelView.scrollSpace = 10; + scrollLabelView.font = [UIFont systemFontOfSize:15]; + scrollLabelView.textAlignment = NSTextAlignmentCenter; + scrollLabelView.backgroundColor = [UIColor blackColor]; + scrollLabelView.layer.cornerRadius = 5; + + /** Step5: 开始滚动(Start scrolling!) */ + [scrollLabelView beginScrolling]; + +} + +- (void)scrollLabelView:(TXScrollLabelView *)scrollLabelView didClickWithText:(NSString *)text{ + NSLog(@"%@",text); +} @end