文字跑马灯
文字跑马灯
#import <UIKit/UIKit.h> @interface UXLabel : UIView { float num; CGRect sFrame; } @property(nonatomic,retain)NSString* string; @property(nonatomic,assign)CGRect stringColor; @property(nonatomic,assign)CGFloat stringFont; -(void)updateLabel; @end // // UXLabel.m // 掌握健康3.0 // // Created by 夏科杰 on 13-9-10. // Copyright (c) 2013年 卓健科技. All rights reserved. // #import "UXLabel.h" @implementation UXLabel @synthesize string,stringColor,stringFont; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { num=0; sFrame=frame; // Initialization code } return self; } - (void)drawRect:(CGRect)rect { num+=0.8; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 1.0); CGContextSetRGBFillColor (context, stringColor.origin.x, stringColor.origin.y, stringColor.size.width, stringColor.size.height); UIFont *font = [UIFont boldSystemFontOfSize:stringFont]; [string drawInRect:CGRectMake(num,sFrame.origin.x, sFrame.size.width, sFrame.size.height) withFont:font]; float max=[string sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, sFrame.size.height)].width+10; num=num>max?-max:num; } -(void)updateLabel { [self setNeedsDisplay]; [self performSelector:@selector(updateLabel) withObject:nil afterDelay:0.1]; } @end 调用方法 UXLabel* label=[[UXLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; label.backgroundColor=[UIColor clearColor]; label.stringColor=CGRectMake(0, 0, 0, 1); label.string=@"卓健信息科技有限公司"; label.stringFont=18.0; [self.view addSubview:label]; [label updateLabel];
Tag : 文字跑马灯,文字滚动
-
前面的方法改一下 #import <UIKit/UIKit.h> @interface UXLabel : UIView { float num; float max; CGRect sFrame; } @property(nonatomic,retain)NSString* string; @property(nonatomic,assign)CGRect stringColor; @property(nonatomic,assign)CGFloat stringFont; -(void)updateLabel; @end // Created by 夏科杰 on 13-9-10. // Copyright (c) 2013年 卓健科技. All rights reserved. // #import "UXLabel.h" @implementation UXLabel @synthesize string,stringColor,stringFont; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { num=0; sFrame=frame; // Initialization code } return self; } - (void)drawRect:(CGRect)rect { num+=0.5; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 1.0); CGContextSetRGBFillColor (context, stringColor.origin.x, stringColor.origin.y, stringColor.size.width, stringColor.size.height); UIFont *font = [UIFont boldSystemFontOfSize:stringFont]; [string drawInRect:CGRectMake(num,0, 320, sFrame.size.height) withFont:font]; num=num>sFrame.size.width?-max:num; } -(void)updateLabel { max=[string sizeWithFont:[UIFont boldSystemFontOfSize:stringFont] constrainedToSize:CGSizeMake(MAXFLOAT, sFrame.size.height)].width; if(max<sFrame.size.width){return;} [self setNeedsDisplay]; [self performSelector:@selector(updateLabel) withObject:nil afterDelay:0.1]; } @end