想了想,最簡單的方法是使用多個UILabel排列顯示,但是這樣不僅麻煩而且效果也不好,索性自定義UILabel來盡可能的滿足使用靈活性。
實現方法
與正常自定義控件的方法類似,主要利用了CoreGraphics來動態繪制字體,但這裡字體的參數都用NSArray存儲,以盡最大可能不受具體內容約束,實現靈活性。
代碼如下:
//
// 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 empty 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],
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];
return self;
}
@end
Demo:
使用方法很簡單,直接在需要使用的地方調用,如下:
- (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 valueWithCGRect: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];
}