diff --git a/TXScrollLabelView/TXScrollLabelView.h b/TXScrollLabelView/TXScrollLabelView.h index 7d20124..e593d2f 100644 --- a/TXScrollLabelView/TXScrollLabelView.h +++ b/TXScrollLabelView/TXScrollLabelView.h @@ -64,6 +64,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 b7b97fc..06a648d 100644 --- a/TXScrollLabelView/TXScrollLabelView.m +++ b/TXScrollLabelView/TXScrollLabelView.m @@ -352,6 +352,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