У меня есть текст на этикетке, который не отображается полностью, поэтому я хочу, чтобы текст на этикетке прокручивался слева направо. Пожалуйста, не предлагайте свойство numberoflines в качестве решения.
как прокручивать текст на этикетке, например, в рамке
comment
Но я предполагаю, что он хочет, чтобы текст прокручивался автоматически?
- person occulus   schedule 28.02.2011
Ответы (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]];
}
}
person
John Dumais
schedule
18.07.2012
Разве мы не можем реализовать textAlignment и textcolor с помощью этого кода ›?
- person Praveenkumar; 13.03.2013
если u появляется в метке целиком, используя эту строку, CGSize labelize = [label.text sizeWithFont: label.font]; затем в CGRectMark (10,10, label.width, 100);
marquee основаны на размере содержимого прокрутки, если вы устанавливаете размер содержимого (200 300), это означает, что вы берете это значение для сравнения со значениями x и y, чтобы установить область выделения.
рассматривать CNSivakumar
person
CNSivakumar
schedule
28.02.2011