Iphone 如何在标签上滚动文本,如选框
我的标签上的文字并没有完全显示出来,所以我希望文字在标签上从左向右滚动。请不要建议使用numberoflines属性作为解决方案 如果使用此行在标签中显示完整的字符串 CGSize labelsize=[label.text sizeWithFont:label.font]; 然后在CGRectMark中(10,10,标签宽度,100) 选框基于滚动内容大小,如果u设置内容大小(200300),则取该值与x和y值进行比较以设置选框 重视Iphone 如何在标签上滚动文本,如选框,iphone,objective-c,text,marquee,Iphone,Objective C,Text,Marquee,我的标签上的文字并没有完全显示出来,所以我希望文字在标签上从左向右滚动。请不要建议使用numberoflines属性作为解决方案 如果使用此行在标签中显示完整的字符串 CGSize labelsize=[label.text sizeWithFont:label.font]; 然后在CGRectMark中(10,10,标签宽度,100) 选框基于滚动内容大小,如果u设置内容大小(200300),则取该值与x和y值进行比较以设置选框 重视 CNSivakumarFWIW,我在滚动视图中使用了两个标
CNSivakumarFWIW,我在滚动视图中使用了两个标签做了类似的事情
#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]];
}
}
#导入
@界面滚动文本视图:UIScrollView
{
UILabel*label1;
UILabel*label2;
NSString*\u文本;
NSString*newText;
CGFloat文本宽度;
布尔动画;
BOOL需要更新文本;
}
@属性(非原子,复制,setter=setText:)NSString*text;
@属性(非原子,赋值)NSTimeInterval scrollInterval;
@属性(非原子,赋值)NSTimeInterval滚动持续时间;
-(id)initWithFrame:(CGRect)frame和text:(NSString*)text;
-(void)setText:(NSString*)文本;
-(BOOL)textFitsInFrame;
@实现滚动文本视图
@综合文本=_文本;
@持续时间;
@合成滚动间隔;
-(void)adjustLabelsForNewText:(NSString*)文本和ParentFrame:(CGRect)框架
{
_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]];
如果([self-textFitsInFrame])
{
[label2从SuperView中移除];
}
其他的
{
[label2 setFrame:CGRectMake([label1 frame].size.width,0.0,textWidth,frame.size.height)];
[label2 setText:[self text]];
如果(![[self子视图]包含对象:label2])
{
[自添加子视图:label2];
}
[自鸣得意];
}
}
-(id)initWithFrame:(CGRect)frame和text:(NSString*)text
{
self=[super initWithFrame:frame];
如果(自我){
[self-setClipsToBounds:是];
设置动画=否;
needToUpdateText=否;
[自设置CrollDuration:10.0];
[自设置Crollinterval:2.0];
label1=[UILabel new];
label2=[UILabel new];
[label1 setBackgroundColor:[UIColor clearColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[自添加子视图:label1];
[新文本的自调整标签:文本和父帧:帧];
}
回归自我;
}
-(void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context
{
如果(需要更新文本)
{
设置动画=否;
needToUpdateText=否;
[self-adjustLabelsForNewText:@“和父帧:[自帧]];
[新文本的自调整标签:新文本和父帧:[自帧]];
newText=nil;
返回;
}
[self-setContentOffset:CGPointMake(0.0,0.0)];
[自鸣得意];
}
-(无效)开始滚动
{
设置动画=是;
[UIView beginAnimations:nil上下文:nil];
[UIView setAnimationDuration:[self-scrollDuration]];
[UIView setAnimationDelay:[自滚动间隔]];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView设置动画曲线:UIViewAnimationCurveLinear];
[self-setContentOffset:CGPointMake(textWidth,0.0)];
[UIView委员会];
}
-(BOOL)textFitsInFrame
{
返回textWidth,但我假设他希望文本自动滚动?我们不能用此代码实现textAlignment和textcolor>?