IOS Development notes-Implementation of UILabel mixed with any font and color

Source: Internet
Author: User

IOS Development notes-Implementation of UILabel mixed with any font and color
The simplest way is to use multiple UILabel to arrange and display, but this is not only troublesome but also ineffective, and the custom UILabel is as flexible as possible. The implementation method is similar to the normal custom control method. CoreGraphics is mainly used to dynamically draw fonts. However, the font parameters here are stored in NSArray, so as not to be subject to specific content constraints as much as possible, flexibility. 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> @ interface UnevenHeightLabel: 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 "@ implementation UnevenHeightLabel // Only override drawRect: if you perform custom drawing. // An emp Ty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {// Drawing code CGContextRef ctx = UIGraphicsGetCurrentContext (); CGContextSetLineWidth (ctx, 1.0f); if (_ strings! = Nil & _ strings. count> 0) {for (int I = 0; I <_ strings. count; I ++) {[_ strings [I] drawInRect: [(NSValue *) _ fontFrames [I] CGRectValue] withAttributes: @ {NSFontAttributeName: _ fonts [I], events: _ fontColors [I]}] ;}}- (instancetype) Events :( 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]; return self ;}@ end Demo: it is easy to use and called directly wherever needed, as shown below:-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 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] stringRects: @ [[NSValue valueWithCGRect: CGRectMake (0, 0, 40, 30)], [NSValue restart: CGRectMake (20, 5, 20, 20)], [NSValue valueWithCGRect: CGRectMake (35, 0, 25, 30)], [NSValue valueWithCGRect: CGRectMake (55, 5, 20, 20)], [NSValue valueWithCGRect: CGRectMake (70, 0, 25, 30)], [NSValue valueWithCGRect: CGRectMake (90, 5, 20, 20)] 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]; UnevenHeightLabel * mylabel1 = [[UnevenHeightLabel alloc] initWithUnevenHeightStrings: @ [@ "11. ", @" 0 ", @" % "] stringFonts: @ [[UIFont systemFontOfSize: 25], [UIFont systemFontOfSize: 20], [UIFont systemFontOfSize: 25] stringRects: @ [[NSValue valueWithCGRect: CGRectMake (0, 0, 40, 30)], [NSValue valueWithCGRect: CGRectMake (35, 5, 20, 20)], [NSValue valueWithCGRect: CGRectMake (50, 0, 25, 30)] stringColors: @ [[UIColor getColorFromHex: @ "# ff8946"], [UIColor getColorFromHex: @ "# ff8946"], [UIColor getColorFromHex: @ "# ff8946"]; [mylabel1 setFrame: CGRectMake (SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2 + 30,100, 50)]; [mylabel1 setBackgroundColor: [UIColor clearColor]; [self. view addSubview: mylabel1];}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.