Recently, a new app has been developed, and a label with a different size and font is shown on the image of a prostitute lion, like this:
Think about it, the simplest way is to use multiple Uilabel to arrange the display, but it is not only troublesome and ineffective, simply customize the Uilabel to meet the use flexibility as much as possible.
Implementation method
Similar to normal custom controls, the core graphics are usedto dynamically draw fonts, but the parameters of the fonts are stored in Nsarray to maximize the flexibility of being independent of specific content.
The code is as follows:
////UnevenHeightLabel.h//Demo////Created by Zhangchangwei on 15/3/31.//Copyright (c) 2015 Changwei. All rights reserved.//#import<UIKit/UIKit.h>@interfaceUnevenheightlabel:uiview@property (nonatomic) Nsarray*strings, @property (nonatomic) Nsarray*fonts, @property (nonatomic) Nsarray*Fontframes, @property (nonatomic) Nsarray*fontcolors;-(Instancetype) Initwithunevenheightstrings: (Nsarray *) strings stringfonts: (Nsarray *) fonts stringrects: (NSArray *) Rects stringcolors: (Nsarray *) colors;@end
////UNEVENHEIGHTLABEL.M//Demo////Created by Zhangchangwei on 15/3/31.//Copyright (c) 2015 Changwei. All rights reserved.//#import "UnevenHeightLabel.h"#import "Uicolor+hexcolor.h"@implementationUnevenheightlabel//Only override Drawrect:if perform custom drawing.//An empty implementation adversely affects performance during animation.- (void) DrawRect: (cgrect) rect {//Drawing CodeCgcontextref CTX =Uigraphicsgetcurrentcontext (); Cgcontextsetlinewidth (CTX,1.0f); if(_strings!=nil&& _strings.count>0){ for(intI=0; i<_strings.count; i++) {[_strings[i] drawinrect:[(Nsvalue*) _fontframes[i] cgrectvalue] withattributes:@{nsfontattributename:_fonts[i], Nsforegroundcolorattributename:_fontcolors[i]}]; } }}-(Instancetype) Initwithunevenheightstrings: (Nsarray *) strings stringfonts: (Nsarray *) fonts stringrects: (NSArray *) Rects stringcolors: (Nsarray *) colors{ Self=[Super Init]; Self.strings=strings; Self.fonts=fonts; Self.fontframes=rects; Self.fontcolors=colors; //[self setneedsdisplay]; returnSelf ;}@end
Demo:
The use of the method is simple, directly in the need to use the place called, as follows:
- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Unevenheightlabel *mylabel=[[unevenheightlabel Alloc] initwithunevenheightstrings:@[@"A",@"a",@"B",@"b",@"C",@"C"] Stringfonts:@[[uifont systemfontofsize: -], [Uifont systemfontofsize: -], [Uifont systemfontofsize: -], [Uifont systemfontofsize: -], [Uifont systemfontofsize: -], [Uifont systemfontofsize: -]] stringrects:@[ [Nsvalue Valuewithcgrect:cgrectmake (0,0, +, -)], [Nsvalue VALUEWITHCGRECT:CG Rectmake ( -,5, -, -)], [Nsvalue VALUEWITHCGRECT:CG Rectmake ( *,0, -, -)], [Nsvalue VALUEWITHCGRECT:CG Rectmake ( -,5, -, -)], [Nsvalue VALUEWITHCGRECT:CG Rectmake ( -,0, -, -)], [Nsvalue VALUEWITHCGRECT:CG Rectmake ( -,5, -, -)] [stringcolors:@[] [Uicolor Redcolor], [Uicolor Orangecolor], [Uicolor Greencolor], [Uicolor Bluecolor], [Uicolor Cyancolor], [Uicolo R Purplecolor]]; [MyLabel setframe:cgrectmake (Screen_width/2- -, screen_height/2- -, the, -)]; [MyLabel Setbackgroundcolor:[uicolor Clearcolor]; [Self.view Addsubview:mylabel]; Unevenheightlabel*mylabel1=[[unevenheightlabel Alloc] initwithunevenheightstrings:@[@"One by one .",@"0",@"%"] Stringfonts:@[[uifont systemfontofsize: -], [Uifont systemfontofsize:
-], [Uifont systemfontofsize:
-]] stringrects:@[ [Nsvalue Valuewithcgrect:cgrectmake (0,0, +, -)], [Nsvalue valuewithcgrect:c Grectmake ( *,5, -, -)], [Nsvalue valuewithcgrect:c Grectmake ( -,0, -, -)]] Stringcolors:@[[uicolor getcolorfromhex:< /c1>@"#ff8946"], [Uicolor getcolorfromhex:
@"#ff8946"],[uicolor Getcolorfromhex:@"#ff8946"]]]; [Mylabel1 setframe:cgrectmake (Screen_width/2- -, screen_height/2+ -, -, -)]; [Mylabel1 Setbackgroundcolor:[uicolor Clearcolor]; [Self.view Addsubview:mylabel1];}
The effect is as follows:
iOS Development note-An implementation of any font, color mixed uilabel