2011-02-28 72 views
2

我在標籤上顯示的文字並未完全顯示,因此我希望文字從左至右滾動標籤。請不要將numberoflines屬性建議爲解決方案。如何在標籤上滾動文字,如跑馬燈

+0

使用的UIScrollView – 2011-02-28 10:06:08

+1

但我認爲他想要的文本自動滾動? – occulus 2011-02-28 10:51:46

回答

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