文字跑马灯

文字跑马灯

文字跑马灯
#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 : 文字跑马灯,文字滚动
761 次查看 |  E 创建 | 2013-09-10 11:03:37
  • 前面的方法改一下
    
    #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
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值