UILabel字型加粗等屬性和特效
#import <UIKit/UIKit.h>
@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 "TextFlowView.h"
@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(0, 0);
}
CGSize boundSize = CGSizeMake(10000, 100);
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",rect.origin.x, rect.origin.y);
// NSLog(@"rect W:%f H:%f", rect.size.width, rect.size.height);
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.y) rect:rect];
// NSLog(@"inner->rect X:%f Y:%f",rect.origin.x, rect.origin.y);
// NSLog(@"inner->rect W:%f H:%f", rect.size.width, rect.size.height);
}
}
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