UILabel字体加粗等属性和特效

时间:2025-01-25 09:00:56

///bbs/?tid=74540

#import <UIKit/>

@interface TextFlowView : UIView {

    

    //显示文本的标签

    UILabel *_firstLabel;

    UILabel *_secondLabel;

    

    //定时器

    NSTimer *_timer;

    

    //显示的文本

    NSString *_text;

    

    //是否需要滚动

    BOOL _needFlow;

    

    //控件的框架大小

    CGRect _frame;

    

    //文本的字体

    UIFont *_font;

    

    //当前第一个控件的索引

    NSInteger _startIndex;

    

    //定时器每次执行偏移后,累计的偏移量之和

    CGFloat _XOffset;

    

    //文本显示一行,需要的框架大小

    CGSize _textSize;

    

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text;

- (void)setFont:(UIFont *)font;

- (void)setText:(NSString *)text;

@end

 

//

#import ""

@implementation TextFlowView

#pragma mark -

#pragma mark 内部调用

#define SPACE_WIDTH 50

#define LABEL_NUM 2

//改变一个TRect的起始点位置,但是其终止店点的位置不变,因此会导致整个框架大小的变化

- (CGRect)moveNewPoint:(CGPoint)point rect:(CGRect)rect

{

    CGSize tmpSize;

    tmpSize.height = rect.size.height + (rect.origin.y - point.y);

    tmpSize.width = rect.size.width + (rect.origin.x - point.x);

    returnCGRectMake(point.x, point.y, tmpSize.width, tmpSize.height);

}

//开启定时器

- (void)startRun

{

    _timer = [NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(timerAction) userInfo:nilrepeats:YES];

}

//关闭定时器

- (void)cancelRun

{

    if (_timer

    {

        [_timerinvalidate];

    }

}

//定时器执行的操作

- (void)timerAction

{

    staticCGFloat offsetOnce = -1;

    _XOffset += offsetOnce;

    if (_XOffset +  _textSize.width <= 0

    {

        _XOffset += _textSize.width;

        _XOffset += SPACE_WIDTH;

    }

    [selfsetNeedsDisplay];

    

}

//计算在给定字体下,文本仅显示一行需要的框架大小

- (CGSize)computeTextSize:(NSString *)text

{

    if (text == nil

    {

        returnCGSizeMake(00);

    }

    CGSize boundSize = CGSizeMake(10000100);

    CGSize stringSize = [_textsizeWithFont:_fontconstrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];

    return stringSize;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text

{

    self = [superinitWithFrame:frame];

    if (self

    {

        _text = [text retain];

        _frame = frame;

        //默认的字体大小

        _font = [UIFontsystemFontOfSize:16.0F];

        self.backgroundColor = [UIColorredColor];

        //初始化标签

        //判断是否需要滚动效果

        _textSize = [selfcomputeTextSize:text];

        //需要滚动效果

        if (_textSize.width > frame.size.width

        {

            _needFlow = YES;

            [selfstartRun];

        }

    }

    returnself;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    CGContextRef context= UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColorwhiteColor].CGColor);

    // Drawing code

    CGFloat startYOffset = (rect.size.height - _textSize.height)/2;

    CGPoint origin = rect.origin;

    if (_needFlow == YES

    {

//        NSLog(@"OFFSETX:%f", _XOffset);

//        NSLog(@"textwidth:%f",_textSize.width);

        rect = [selfmoveNewPoint:CGPointMake(_XOffset, startYOffset) rect:rect];

//        NSLog(@"rect X:%f  Y:%f",, );

//        NSLog(@"rect W:%f  H:%f", , );

        while (rect.origin.x <= rect.size.width+rect.origin.x

        {

            [_textdrawInRect:rect withFont:_font];

            rect = [selfmoveNewPoint:CGPointMake(rect.origin.x+_textSize.width+SPACE_WIDTH, rect.origin.yrect:rect];

//            NSLog(@"inner->rect X:%f  Y:%f",, );

//            NSLog(@"inner->rect W:%f  H:%f", , );

        }

        

    }

    else

    {

        //在控件的中间绘制文本

        origin.x = (rect.size.width - _textSize.width)/2;

        origin.y = (rect.size.height - _textSize.height)/2;

        rect.origin = origin;

        [_textdrawInRect:rect withFont:_font];

    }

}

- (void)dealloc

{

    [_textrelease];

    [superdealloc];

}

#pragma mark -

#pragma mark 外部调用

- (void)setFont:(UIFont *)font

{

    _font = font;

}

- (void)setText:(NSString *)text

{

    [_textrelease];

    _text = [text retain];

}

@end