今天分享自己掌握的几种UIlabel ,大家有更多的label请分享给我哟
1:内边距,自适应的label ,这个label主要是用于内边距同时要Masonry进行适配的时候使用
InsetLabel.h
@interfaceInsetLabel :UILabel
//用于设置Label的内边距
@property(nonatomic)UIEdgeInsetsneiinsets;
//初始化方法一
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) neiinsets;
//初始化方法二
-(id) initWithInsets: (UIEdgeInsets) neiinsets;
@end
InsetLabel.m
#import"InsetLabel.h"
@implementationInsetLabel
//初始化方法一
-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)neiinsets {
self= [superinitWithFrame:frame];
if(self){
self.neiinsets= neiinsets;
}
returnself;
}
//初始化方法二
-(id) initWithInsets:(UIEdgeInsets)neiinsets {
self= [superinit];
if(self){
self.neiinsets= neiinsets;
}
returnself;
}
//重载drawTextInRect方法
-(void) drawTextInRect:(CGRect)rect {
return[superdrawTextInRect:UIEdgeInsetsInsetRect(rect,self.neiinsets)];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
UIEdgeInsetsinsets =self.neiinsets;
CGRectrect = [supertextRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
limitedToNumberOfLines:numberOfLines];
rect.origin.x-= insets.left;
rect.origin.y-= insets.top;
rect.size.width+= (insets.left+ insets.right);
rect.size.height+= (insets.top+ insets.bottom);
returnrect;
}
@end
2:描边的label ,以及虚线label
WJFOutlineLabel.h
@interfaceWJFOutlineLabel : UILabel
/**描多粗的边*/
@property(nonatomic,assign) NSInteger outLineWidth;
/**外轮颜色*/
@property(nonatomic,strong) UIColor *outLinetextColor;
/**里面字体默认颜色*/
@property(nonatomic,strong) UIColor *labelTextColor;
@end
WJFOutlineLabel.m
#import"WJFOutlineLabel.h"
@implementationWJFOutlineLabel
- (void)drawTextInRect:(CGRect)rect {
CGSize shadowOffset =self.shadowOffset;
UIColor *textColor =self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c,self.outLineWidth);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor =self.outLinetextColor;
[superdrawTextInRect:rect];
self.textColor = textColor;
CGContextSetTextDrawingMode(c, kCGTextFill);
[superdrawTextInRect:rect];
self.shadowOffset = shadowOffset;
}
//虚线的label
-(void)dottedLineWithView:(UIView*)dottedView{
CAShapeLayer*border = [CAShapeLayerlayer];
border.strokeColor= [UIColorredColor].CGColor;
border.fillColor=nil;
border.path= [UIBezierPathbezierPathWithRect:dottedView.bounds].CGPath;
border.frame= dottedView.bounds;
border.lineWidth=1.f;
border.lineCap=@"square";
border.lineDashPattern=@[@4,@2];
[dottedView.layeraddSublayer:border];
}
@end
3:辉光效果的Label
WJFlowLabel.h
@interfaceWJFlowLabel :UILabel
/**
*Glow size, default is 0.f.
*/
@property(nonatomic)CGFloatglowSize;
/**
*Glow color, default is clear color.
*/
@property(nonatomic,strong)UIColor*glowColor;
/**
*Inner glow size, default is 0.f.
*/
@property(nonatomic)CGFloatinnerGlowSize;
/**
*Inner glow color, default is clear color.
*/
@property(nonatomic,strong)UIColor*innerGlowColor;
@end
WJFlowLabel.m
#import"WJFlowLabel.h"
@implementationWJFlowLabel
- (id)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
[selfsetup];
}
returnself;
}
- (id)initWithCoder:(NSCoder*)coder {
if(self= [superinitWithCoder:coder]) {
[selfsetup];
}
returnself;
}
- (void)setup {
self.glowSize=0.0f;
self.glowColor= [UIColorclearColor];
self.innerGlowSize=0.0f;
self.innerGlowColor= [UIColorclearColor];
}
- (void)drawTextInRectForIOS7:(CGRect)rect {
CGContextRefctx =UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
[superdrawTextInRect:rect];
UIImage*textImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextSaveGState(ctx);
if(_glowSize>0) {
CGContextSetShadow(ctx,CGSizeZero,_glowSize);
CGContextSetShadowWithColor(ctx,CGSizeZero,_glowSize,_glowColor.CGColor);
}
[textImagedrawAtPoint:rect.origin];
CGContextRestoreGState(ctx);
if(_innerGlowSize>0) {
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
CGContextRefctx2 =UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx2);
CGContextSetFillColorWithColor(ctx2, [UIColorblackColor].CGColor);
CGContextFillRect(ctx2, rect);
CGContextTranslateCTM(ctx2,0.0, rect.size.height);
CGContextScaleCTM(ctx2,1.0, -1.0);
CGContextClipToMask(ctx2, rect, textImage.CGImage);
CGContextClearRect(ctx2, rect);
CGContextRestoreGState(ctx2);
UIImage*inverted =UIGraphicsGetImageFromCurrentImageContext();
CGContextClearRect(ctx2, rect);
CGContextSaveGState(ctx2);
CGContextSetFillColorWithColor(ctx2,_innerGlowColor.CGColor);
CGContextSetShadowWithColor(ctx2,CGSizeZero,_innerGlowSize,_innerGlowColor.CGColor);
[inverteddrawAtPoint:CGPointZero];
CGContextTranslateCTM(ctx2,0.0, rect.size.height);
CGContextScaleCTM(ctx2,1.0, -1.0);
CGContextClipToMask(ctx2, rect, inverted.CGImage);
CGContextClearRect(ctx2, rect);
CGContextRestoreGState(ctx2);
UIImage*innerShadow =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[innerShadowdrawAtPoint:rect.origin];
}
}
- (void)drawTextInRectForIOS6:(CGRect)rect {
CGContextRefctx =UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
if(self.glowSize>0) {
CGContextSetShadow(ctx,CGSizeZero,_glowSize);
CGContextSetShadowWithColor(ctx,CGSizeZero,_glowSize,_glowColor.CGColor);
}
[superdrawTextInRect:rect];
CGContextRestoreGState(ctx);
if(_innerGlowSize>0) {
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
CGContextRefctx2 =UIGraphicsGetCurrentContext();
[superdrawTextInRect:rect];
UIImage*textImage =UIGraphicsGetImageFromCurrentImageContext();
CGContextClearRect(ctx2, rect);
CGContextSaveGState(ctx2);
CGContextSetFillColorWithColor(ctx2, [UIColorblackColor].CGColor);
CGContextFillRect(ctx2, rect);
CGContextTranslateCTM(ctx2,0.0, rect.size.height);
CGContextScaleCTM(ctx2,1.0, -1.0);
CGContextClipToMask(ctx2, rect, textImage.CGImage);
CGContextClearRect(ctx2, rect);
CGContextRestoreGState(ctx2);
UIImage*inverted =UIGraphicsGetImageFromCurrentImageContext();
CGContextClearRect(ctx2, rect);
CGContextSaveGState(ctx2);
CGContextSetFillColorWithColor(ctx2,_innerGlowColor.CGColor);
CGContextSetShadowWithColor(ctx2,CGSizeZero,_innerGlowSize,_innerGlowColor.CGColor);
[inverteddrawAtPoint:CGPointZero];
CGContextTranslateCTM(ctx2,0.0, rect.size.height);
CGContextScaleCTM(ctx2,1.0, -1.0);
CGContextClipToMask(ctx2, rect, inverted.CGImage);
CGContextClearRect(ctx2, rect);
CGContextRestoreGState(ctx2);
UIImage*innerShadow =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[innerShadowdrawAtPoint:rect.origin];
}
}
- (void)drawTextInRect:(CGRect)rect {
if(self.text==nil||self.text.length==0) {
return;
}
if([[[UIDevicecurrentDevice]systemVersion]compare:@"7.0"options:NSNumericSearch] !=NSOrderedAscending) {
[selfdrawTextInRectForIOS7:rect];
}else{
[selfdrawTextInRectForIOS6:rect];
}
}
@end
4:广告UIlabel
BBCyclingLabel.h
#pragma mark - Enums
typedefenum
{
// User must provide pre-transition and transition blocks
BBCyclingLabelTransitionEffectCustom =0,
BBCyclingLabelTransitionEffectFadeIn=1<<0,
BBCyclingLabelTransitionEffectFadeOut=1<<1,
BBCyclingLabelTransitionEffectCrossFade =BBCyclingLabelTransitionEffectFadeIn|
BBCyclingLabelTransitionEffectFadeOut,
BBCyclingLabelTransitionEffectZoomIn=1<<2,
BBCyclingLabelTransitionEffectZoomOut =1<<3,
BBCyclingLabelTransitionEffectScaleFadeOut =BBCyclingLabelTransitionEffectFadeIn|
BBCyclingLabelTransitionEffectFadeOut|
BBCyclingLabelTransitionEffectZoomOut,
BBCyclingLabelTransitionEffectScaleFadeIn=BBCyclingLabelTransitionEffectFadeIn|
BBCyclingLabelTransitionEffectFadeOut|
BBCyclingLabelTransitionEffectZoomIn,
// These two move the entering label from above/below to center and exiting label up/down without cross-fade
// It's a good idea to set the clipsToBounds property of the BBCyclingLabel to true and use this in a confined space
BBCyclingLabelTransitionEffectScrollUp=1<<4,
BBCyclingLabelTransitionEffectScrollDown =1<<5,
BBCyclingLabelTransitionEffectDefault =BBCyclingLabelTransitionEffectCrossFade
} BBCyclingLabelTransitionEffect;
#pragma mark - Custom types
typedefvoid(^BBCyclingLabelPreTransitionBlock)(UILabel* labelToEnter);
typedefvoid(^BBCyclingLabelTransitionBlock)(UILabel* labelToExit,UILabel* labelToEnter);
#pragma mark -
@interfaceBBCyclingLabel :UIView
#pragma mark Public properties
@property(assign,nonatomic)BBCyclingLabelTransitionEffecttransitionEffect;
@property(copy,nonatomic)BBCyclingLabelPreTransitionBlockpreTransitionBlock;
@property(copy,nonatomic)BBCyclingLabelTransitionBlocktransitionBlock;
@property(assign,nonatomic)NSTimeIntervaltransitionDuration;
// Same properties as UILabel, these will be propagated to the underlying labels
@property(copy,nonatomic)NSString*text;
@property(strong,nonatomic)UIFont*font;
@property(strong,nonatomic)UIColor*textColor;
@property(strong,nonatomic)UIColor*shadowColor;
@property(assign,nonatomic)CGSizeshadowOffset;
@property(assign,nonatomic)UITextAlignmenttextAlignment;
@property(assign,nonatomic)UILineBreakModelineBreakMode;
@property(assign,nonatomic)NSIntegernumberOfLines;
@property(assign,nonatomic)BOOLadjustsFontSizeToFitWidth;
@property(assign,nonatomic)CGFloatminimumFontSize;
@property(assign,nonatomic)UIBaselineAdjustmentbaselineAdjustment;
#pragma mark Creation
- (id)initWithFrame:(CGRect)frame andTransitionType:(BBCyclingLabelTransitionEffect)transitionEffect;
#pragma mark Public methods
/*! Sets the text for the next label and performs a transition between current and next label (if animated is YES) */
- (void)setText:(NSString*)text animated:(BOOL)animated;
@end
BBCyclingLabel.m
#import"BBCyclingLabel.h"
#pragma mark - Constants
NSTimeIntervalconstkBBCyclingLabelDefaultTransitionDuration =0.3;
#pragma mark -
@interfaceBBCyclingLabel()
{
NSUInteger_currentLabelIndex;
}
#pragma mark Private properties
@property(strong,nonatomic)NSArray* labels;
@property(strong,nonatomic)UILabel* currentLabel;
#pragma mark Private helpers
- (void)setupWithEffect:(BBCyclingLabelTransitionEffect)effect andDuration:(NSTimeInterval)duration;
- (void)prepareTransitionBlocks;
- (NSUInteger)nextLabelIndex;
- (void)resetLabel:(UILabel*)label;
@end
#pragma mark -
@implementationBBCyclingLabel
#pragma mark Property synthesizers
@synthesizetransitionEffect=_transitionEffect;
@synthesizepreTransitionBlock =_preTransitionBlock;
@synthesizetransitionBlock=_transitionBlock;
@synthesizetransitionDuration =_transitionDuration;
// Private
@synthesizelabels=_labels;
@synthesizecurrentLabel =_currentLabel;
#pragma mark Creation
- (id)initWithFrame:(CGRect)frame
{
self= [superinitWithFrame:frame];
if(self!=nil) {
[selfsetupWithEffect:BBCyclingLabelTransitionEffectDefault
andDuration:kBBCyclingLabelDefaultTransitionDuration];
}
returnself;
}
- (id)initWithCoder:(NSCoder*)coder
{
self= [superinitWithCoder:coder];
if(self!=nil) {
[selfsetupWithEffect:BBCyclingLabelTransitionEffectDefault
andDuration:kBBCyclingLabelDefaultTransitionDuration];
}
returnself;
}
- (id)initWithFrame:(CGRect)frame andTransitionType:(BBCyclingLabelTransitionEffect)transitionEffect;
{
self= [superinitWithFrame:frame];
if(self!=nil) {
[selfsetupWithEffect:transitionEffect
andDuration:kBBCyclingLabelDefaultTransitionDuration];
}
returnself;
}
#pragma mark Manual property accessors
- (void)setTransitionEffect:(BBCyclingLabelTransitionEffect)transitionEffect
{
_transitionEffect= transitionEffect;
[selfprepareTransitionBlocks];
}
- (NSString*)text
{
return_currentLabel.text;
}
- (void)setText:(NSString*)text
{
[selfsetText:textanimated:YES];
}
- (UIFont*)font
{
return_currentLabel.font;
}
- (void)setFont:(UIFont*)font
{
for(UILabel* labelin_labels) {
label.font= font;
}
}
- (UIColor*)textColor
{
return_currentLabel.textColor;
}
- (void)setTextColor:(UIColor*)textColor
{
for(UILabel* labelin_labels) {
label.textColor= textColor;
}
}
- (UIColor*)shadowColor
{
return_currentLabel.shadowColor;
}
- (void)setShadowColor:(UIColor*)shadowColor
{
for(UILabel* labelin_labels) {
label.shadowColor= shadowColor;
}
}
- (CGSize)shadowOffset
{
return_currentLabel.shadowOffset;
}
- (void)setShadowOffset:(CGSize)shadowOffset
{
for(UILabel* labelin_labels) {
label.shadowOffset= shadowOffset;
}
}
- (UITextAlignment)textAlignment
{
return_currentLabel.textAlignment;
}
- (void)setTextAlignment:(UITextAlignment)textAlignment
{
for(UILabel* labelin_labels) {
label.textAlignment= textAlignment;
}
}
- (UILineBreakMode)lineBreakMode
{
return_currentLabel.lineBreakMode;
}
- (void)setLineBreakMode:(UILineBreakMode)lineBreakMode
{
for(UILabel* labelin_labels) {
label.lineBreakMode= lineBreakMode;
}
}
- (NSInteger)numberOfLines
{
return_currentLabel.numberOfLines;
}
- (void)setNumberOfLines:(NSInteger)numberOfLines
{
for(UILabel* labelin_labels) {
label.numberOfLines= numberOfLines;
}
}
- (BOOL)adjustsFontSizeToFitWidth
{
return_currentLabel.adjustsFontSizeToFitWidth;
}
- (void)setAdjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth
{
for(UILabel* labelin_labels) {
label.adjustsFontSizeToFitWidth= adjustsFontSizeToFitWidth;
}
}
- (CGFloat)minimumFontSize
{
return_currentLabel.minimumFontSize;
}
- (void)setMinimumFontSize:(CGFloat)minimumFontSize
{
for(UILabel* labelin_labels) {
label.minimumFontSize= minimumFontSize;
}
}
- (UIBaselineAdjustment)baselineAdjustment
{
return_currentLabel.baselineAdjustment;
}
- (void)setBaselineAdjustment:(UIBaselineAdjustment)baselineAdjustment
{
for(UILabel* labelin_labels) {
label.baselineAdjustment= baselineAdjustment;
}
}
#pragma mark Public methods
- (void)setText:(NSString*)text animated:(BOOL)animated
{
NSUIntegernextLabelIndex = [selfnextLabelIndex];
UILabel* nextLabel = [_labelsobjectAtIndex:nextLabelIndex];
UILabel* previousLabel =_currentLabel;
nextLabel.text= text;
// Resetting the label state ensures we can change the transition type without extra code on pre-transition block.
// Without it a transition that has no alpha changes would have to ensure alpha = 1 on pre-transition block (as
// well as with every other possible animatable property)
[selfresetLabel:nextLabel];
// Update both current label index and current label pointer
self.currentLabel= nextLabel;
_currentLabelIndex= nextLabelIndex;
// Prepare the next label before the transition animation
if(_preTransitionBlock!=nil) {
_preTransitionBlock(nextLabel);
}else{
// If no pre-transition block is set, prepare the next label for a cross-fade
nextLabel.alpha=0;
}
// Unhide the label that's about to be shown
nextLabel.hidden=NO;
void(^changeBlock)() = ^() {
// Perform the user provided changes
if(_transitionBlock!=nil) {
_transitionBlock(previousLabel, nextLabel);
}else{
// If no transition block is set, perform a simple cross-fade
previousLabel.alpha=0;
nextLabel.alpha=1;
}
};
void(^completionBlock)(BOOL) = ^(BOOLfinished) {
if(finished) {
// TODO this is kind of bugged since all transitions that include affine transforms always return finished
// as true, even when it doesn't finish...
previousLabel.hidden=YES;
}
};
if(animated) {
// Animate the transition between both labels
[UIViewanimateWithDuration:_transitionDurationanimations:changeBlockcompletion:completionBlock];
}else{
changeBlock();
completionBlock(YES);
}
}
#pragma mark Private helpers
- (void)setupWithEffect:(BBCyclingLabelTransitionEffect)effect andDuration:(NSTimeInterval)duration
{
NSUIntegersize =2;
NSMutableArray* labels = [NSMutableArrayarrayWithCapacity:size];
for(NSUIntegeri =0; i < size; i++) {
UILabel* label = [[UILabelalloc]initWithFrame:self.bounds];
[selfaddSubview:label];
label.backgroundColor= [UIColorclearColor];
label.hidden=YES;
label.numberOfLines=0;
[labelsaddObject:label];
}
_currentLabelIndex=0;
self.currentLabel= [labelsobjectAtIndex:0];
self.labels= labels;
_currentLabel.hidden=NO;
self.transitionEffect= effect;
self.transitionDuration= duration;
}
- (void)prepareTransitionBlocks
{
//if matches custom
if(_transitionEffect==BBCyclingLabelTransitionEffectCustom) {
return;
}
BBCyclingLabelTransitionEffecttype =_transitionEffect;
self.preTransitionBlock= ^(UILabel* labelToEnter) {
if(type &BBCyclingLabelTransitionEffectFadeIn) {
labelToEnter.alpha=0;
}
if(type &BBCyclingLabelTransitionEffectZoomIn) {
labelToEnter.transform=CGAffineTransformMakeScale(0.5,0.5);
}
if(type & (BBCyclingLabelTransitionEffectScrollUp|BBCyclingLabelTransitionEffectScrollDown)) {
CGRectframe = labelToEnter.frame;
if(type &BBCyclingLabelTransitionEffectScrollUp) {
frame.origin.y=self.bounds.size.height;
}
if(type &BBCyclingLabelTransitionEffectScrollDown) {
frame.origin.y=0- frame.size.height;
}
labelToEnter.frame= frame;
}
};
self.transitionBlock= ^(UILabel* labelToExit,UILabel* labelToEnter) {
if(type &BBCyclingLabelTransitionEffectFadeIn) {
labelToEnter.alpha=1;
}
if(type &BBCyclingLabelTransitionEffectFadeOut) {
labelToExit.alpha=0;
}
if(type &BBCyclingLabelTransitionEffectZoomOut) {
labelToExit.transform=CGAffineTransformMakeScale(1.5,1.5);
}
if(type &BBCyclingLabelTransitionEffectZoomIn) {
labelToEnter.transform=CGAffineTransformIdentity;
}
if(type & (BBCyclingLabelTransitionEffectScrollUp|BBCyclingLabelTransitionEffectScrollDown)) {
CGRectframe = labelToExit.frame;
CGRectenterFrame = labelToEnter.frame;
if(type &BBCyclingLabelTransitionEffectScrollUp) {
frame.origin.y=0- frame.size.height;
enterFrame.origin.y=roundf((self.bounds.size.height/2) - (enterFrame.size.height/2));
}
if(type &BBCyclingLabelTransitionEffectScrollDown) {
frame.origin.y=self.bounds.size.height;
enterFrame.origin.y=roundf((self.bounds.size.height/2) - (enterFrame.size.height/2));
}
labelToExit.frame= frame;
labelToEnter.frame= enterFrame;
}
};
}
- (NSUInteger)nextLabelIndex
{
return(_currentLabelIndex+1) % [_labelscount];
}
- (void)resetLabel:(UILabel*)label
{
label.alpha=1;
label.transform=CGAffineTransformIdentity;
label.frame=self.bounds;
}
@end
先写这几个了,大家有好的UIlabel可以分享出来哈.