? ? ? ?項目中文字展示不全 老板要求文字在label上滾動 查了很多資料 最后找到李明杰老師的一個demo 封裝的很好 我就在他的基礎(chǔ)上 根據(jù)自己的需求 封裝了一個方法 需要的時候可以直接調(diào)用 次方法支持左右滾動 可以調(diào)節(jié)速度 支持連續(xù)滾動 間斷滾動 往返滾動 可以更改滾動的的起始位置
? ?效果圖如下

具體實現(xiàn)原理其實就是判斷文字的長度是否大于你定義的view的長度 如果字體長度超過展示的viwe的長度 就創(chuàng)建兩個label ?如果字體長度小于文字長度 就創(chuàng)建一個label ?再加上定時器
#pragma mark - 創(chuàng)建滾動視圖
- (void)startScroll{
if (_text.length == 0) {
return;
}
//初始化滾動字符串label
switch (_currentScrollModel) {
case ZLHTextScrollContinuous:
if (_textWidth > self.frame.size.width) {
if(_currentMoveDirection == ZLHTextScrollMoveLeft){
[self creatLabel1AndLabel2WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height) frame2:CGRectMake(_textWidth, 0, _textWidth, self.frame.size.height)];
}else{
[self creatLabel1AndLabel2WithFrame:CGRectMake(self.frame.size.width-_textWidth,0, _textWidth, self.frame.size.height) frame2:CGRectMake(self.frame.size.width-_textWidth-_textWidth, 0, _textWidth, self.frame.size.height)];
}
}else{//如果字符串的長度小于控件寬度,只創(chuàng)建一個字符串label
if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
[self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
}else{
[self creatLabel1WithFrame:CGRectMake(self.frame.size.width-_textWidth, 0, _textWidth, self.frame.size.height)];
}
}break;
case ZLHTextScrollIntermitent:
{
if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
[self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
}else{
[self creatLabel1WithFrame:CGRectMake(self.frame.size.width - _textWidth, 0,_textWidth, self.frame.size.height)];
}
}break;
case ZLHTextScrollFromOutside:
{
if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
[self creatLabel1WithFrame:CGRectMake(self.frame.size.width, 0, _textWidth, self.frame.size.height)];
}else{
[self creatLabel1WithFrame:CGRectMake(_textWidth, 0, _textWidth, self.frame.size.height)];
}
}break;
case ZLHTextScrollWanering:
{
[self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
} break;
default:
break;
}
//設(shè)置速度,開始滾動 (默認(rèn)為0.03)
[self setMoveSpeed:0.03];
}
在.h文件中?
/**
字符創(chuàng)滾動前端的起始位置
*/
typedef enum {
ZLHTextScrollContinuous,? //從控件內(nèi)開始連續(xù)滾動
ZLHTextScrollIntermitent, //從控件內(nèi)開始間斷滾動
ZLHTextScrollFromOutside, //從控件外開始滾動
ZLHTextScrollWanering,? ? //從控件中往返滾動 (不受設(shè)置方向的影響)
}ZLHTextScrollMode;
/**
字符串移動方向
*/
typedef enum {
ZLHTextScrollMoveLeft,
ZLHTextScrollMoveRight
}ZLHTextScrollMoveDirection;
@interface ZLHScrollTextView : UIView
/**
*進(jìn)行控件的初始化
* @param frame 控件的frame
* @param scrollModel 字符串的滾動模式
* @Param moveDirection 字符串的滾動方向
*/
- (id)initWithFrame:(CGRect)frame textScrollModel:(ZLHTextScrollMode)scrollModel direction:(ZLHTextScrollMoveDirection)moveDirection;
/**
* 更改滾動的字符串
* @param text 字符串的內(nèi)容
* @param color 字符串的顏色
* @param font 字符串的字體
*/
-(void)startScrollWithText:(NSString *)text textColor:(UIColor *)color font:(UIFont *)font;
/**
* 設(shè)置字符串的移動速度
* @param speed 移動速度 取值越小速度越快 取值范圍:0.001~0.1
*/
-(void)setMoveSpeed:(CGFloat)speed;
具體實現(xiàn)方法我就不一一說了 可以查看demo
demo地址為: https://git.oschina.net/huanni/scrolltextView.git