我在标签上显示的文字并未完全显示,因此我希望文字从左至右滚动标签。请不要将numberoflines属性建议为解决方案。如何在标签上滚动文字,如跑马灯
2
A
回答
0
如果你使用这条线在标签中出现完整的字符串 CGSize labelsize = [label.text sizeWithFont:label.font]; 然后在CGRectMark(10,10,label.width,100);
选取框基于滚动内容大小,如果你设置内容大小(200,300),意思是拿这个值与x和y值比较来设置一个选取框。
方面 CNSivakumar
2
FWIW,我没有使用2个标签在滚动视图类似的东西...
#import <UIKit/UIKit.h>
@interface ScrolledTextView : UIScrollView
{
UILabel *label1;
UILabel *label2;
NSString *_text;
NSString *newText;
CGFloat textWidth;
BOOL animating;
BOOL needToUpdateText;
}
@property (nonatomic, copy, setter = setText:) NSString *text;
@property (nonatomic, assign) NSTimeInterval scrollInterval;
@property (nonatomic, assign) NSTimeInterval scrollDuration;
- (id) initWithFrame:(CGRect)frame andText : (NSString*) text;
- (void) setText:(NSString *)text;
- (BOOL) textFitsInFrame;
@implementation ScrolledTextView
@synthesize text = _text;
@synthesize scrollDuration;
@synthesize scrollInterval;
- (void) adjustLabelsForNewText : (NSString*) text andParentFrame : (CGRect) frame
{
_text = [NSString stringWithFormat:@"%@ ", text];
CGSize textSize = [[self text] sizeWithFont:[label1 font] constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:UILineBreakModeClip];
[self setContentSize:CGSizeMake(textSize.width * 2, textSize.height)];
textWidth = textSize.width;
[label1 setFrame:CGRectMake(0.0, 0.0, textWidth, frame.size.height)];
[label1 setText:[self text]];
if([self textFitsInFrame])
{
[label2 removeFromSuperview];
}
else
{
[label2 setFrame:CGRectMake([label1 frame].size.width, 0.0, textWidth, frame.size.height)];
[label2 setText:[self text]];
if(! [[self subviews] containsObject:label2])
{
[self addSubview:label2];
}
[self beginScrolling];
}
}
- (id) initWithFrame:(CGRect)frame andText : (NSString*) text
{
self = [super initWithFrame:frame];
if (self) {
[self setClipsToBounds:YES];
animating = NO;
needToUpdateText = NO;
[self setScrollDuration:10.0];
[self setScrollInterval:2.0];
label1 = [UILabel new];
label2 = [UILabel new];
[label1 setBackgroundColor:[UIColor clearColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[self addSubview:label1];
[self adjustLabelsForNewText:text andParentFrame:frame];
}
return self;
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if(needToUpdateText)
{
animating = NO;
needToUpdateText = NO;
[self adjustLabelsForNewText:@"" andParentFrame:[self frame]];
[self adjustLabelsForNewText:newText andParentFrame:[self frame]];
newText = nil;
return;
}
[self setContentOffset:CGPointMake(0.0, 0.0)];
[self beginScrolling];
}
- (void) beginScrolling
{
animating = YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[self scrollDuration]];
[UIView setAnimationDelay:[self scrollInterval]];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[self setContentOffset:CGPointMake(textWidth, 0.0)];
[UIView commitAnimations];
}
- (BOOL) textFitsInFrame
{
return textWidth <= [self frame].size.width;
}
- (void) setText:(NSString *)text
{
if(animating)
{
newText = text;
needToUpdateText = YES;
}
else
{
[self adjustLabelsForNewText:text andParentFrame:[self frame]];
}
}
+1
我们不能使用此代码实现textAlignment和textcolor>? – Praveenkumar 2013-03-13 06:24:43
相关问题
- 1. UI5 - 如何获得跑马灯文字
- 2. 跑马灯标签的缺点
- 3. iOS中标签的跑马灯效果
- 4. 如何在vb.net中有跑马灯种类的文字?
- 5. 文字跑马灯不工作
- 6. 跑马灯在android系统
- 7. 跑马灯,更改内容dinamically并重新开始滚动
- 8. jQuery的横向滑动像跑马灯
- 9. JQuery的跑马灯滑动的顶部
- 10. jQuery的像跑马灯
- 11. 跑马灯在IE中不工作6
- 12. 跑马灯在模拟器上工作,但不在设备上
- 13. 如何将标题标签中的滚动文字?
- 14. 如何从跑马灯中删除尾部空格?
- 15. 无法获得跑马灯效果
- 16. Jquery跑马灯和手风琴碰撞
- 17. Remy Sharp的Jquery跑马灯用法
- 18. Android TextView - 跑马灯CPU负载
- 19. 的Android TextView的跑马灯编程
- 20. Android的通知跑马灯不工作
- 21. 的Android TextView的跑马灯不工作
- 22. 为HTML网页跑马灯建议
- 23. 跑马灯进度条有问题
- 24. TextView的跑马灯不工作
- 25. 使用CSS有跑马灯效果
- 26. 跑马灯循环不是无限的
- 27. jquery,跑马灯定时器停止/去
- 28. 我如何为每个文本行做跑马灯停止延迟
- 29. 按钮跑马灯停止一旦标题栏变为(帮助)
- 30. 跑马灯更换不上的Safari 5.1.7(视窗10)
使用的UIScrollView – 2011-02-28 10:06:08
但我认为他想要的文本自动滚动? – occulus 2011-02-28 10:51:46