最近開發新App,射妓獅給的圖上出現一種不一樣大小字體混排的Label,就像下面這種:ide
想了想,最簡單的方法是使用多個UILabel排列顯示,可是這樣不只麻煩並且效果也很差,索性自定義UILabel來儘量的知足使用靈活性。字體
實現方法ui
與正常自定義控件的方法相似,主要利用了CoreGraphics來動態繪製字體,但這裏字體的參數都用NSArray存儲,以盡最大可能不受具體內容約束,實現靈活性。atom
代碼以下:spa
#import <UIKit/UIKit.h> @interface UnevenHeightLabel : UIView @property (nonatomic) NSArray *strings; @property (nonatomic) NSArray *fonts; @property (nonatomic) NSArray *originY; @property (nonatomic) NSArray *fontColors; -(instancetype) initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY stringColors:(NSArray *) colors; @end
#import "UnevenHeightLabel.h" #import "UIColor+HexColor.h" @implementation UnevenHeightLabel // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(ctx, 1.0f); CGRect startRect; CGSize requiredSize; float sumWidth=0; if(_strings!=nil&& _strings.count>0){ for (int i=0; i<_strings.count; i++) { CGSize maxSize=rect.size; requiredSize=[_strings[i] boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_fonts[i]} context:nil].size; if(i==0){ startRect=CGRectMake(0, 0, maxSize.width, maxSize.height); } else{ startRect=CGRectMake(sumWidth, [_originY[i] floatValue], requiredSize.width, requiredSize.height); } [_strings[i] drawInRect:startRect withAttributes:@{NSFontAttributeName:_fonts[i], NSForegroundColorAttributeName:_fontColors[i]}]; sumWidth=sumWidth+requiredSize.width; } } } -(instancetype)initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY stringColors:(NSArray *)colors { self=[super init]; self.strings=strings; self.fonts=fonts; self.originY=originY; self.fontColors=colors; //[self setNeedsDisplay]; return self; } @end
Demo:code
使用方法很簡單,直接在須要使用的地方調用,以下:orm
UnevenHeightLabel *label=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"11.",@"00",@"%"] stringFonts:@[[UIFont systemFontOfSize:20],[UIFont systemFontOfSize:25],[UIFont systemFontOfSize:30]] originY:@[@0,@0,@0] stringColors:@[[UIColor redColor],[UIColor blueColor],[UIColor greenColor]]]; label.frame=CGRectMake(100, 100, 200, 30); label.backgroundColor=[UIColor clearColor]; [self.view addSubview:label]; UnevenHeightLabel *mylabel=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"A",@"a",@"B",@"b",@"C",@"c"] stringFonts:@[[UIFont systemFontOfSize:25], [UIFont systemFontOfSize:20], [UIFont systemFontOfSize:25], [UIFont systemFontOfSize:20], [UIFont systemFontOfSize:25], [UIFont systemFontOfSize:20]] originY:@[@0,@0,@0,@0,@0,@0] stringColors:@[ [UIColor redColor], [UIColor orangeColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor]]]; [mylabel setFrame:CGRectMake(SCREEN_WIDTH/2-55, SCREEN_HEIGHT/2-30, 110, 50)]; [mylabel setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:mylabel];
效果以下:blog