要在同一行文字裡混搭不同字型、不同顏色、不同高度,最直白的想法是排列多個 UILabel。但這麼做不但麻煩,對齊效果也不好。與來自訂一個 UILabel 子類,讓它盡量保有使用上的彈性。
做法與一般自訂控件無異,主要是透過 CoreGraphics 在 drawRect: 中動態繪製文字。差別在於——所有繪製參數(文字內容、字型、矩形範圍、顏色)全部改用 NSArray 存放,讓這個控件不被任何具體內容綁死,任何組合都能套用。
// // UnevenHeightLabel.h // #import@interface UnevenHeightLabel : UIView @property (nonatomic) NSArray *strings; // 每一段的文字 @property (nonatomic) NSArray *fonts; // 對應的字型 @property (nonatomic) NSArray *fontFrames; // 對應的繪製範圍(NSValue 包裝的 CGRect) @property (nonatomic) NSArray *fontColors; // 對應的顏色 - (instancetype)initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts stringRects:(NSArray *)rects stringColors:(NSArray *)colors; @end
//
// UnevenHeightLabel.m
//
#import "UnevenHeightLabel.h"
@implementation UnevenHeightLabel
// 自訂繪製時才需要覆寫 drawRect:
- (void)drawRect:(CGRect)rect
{
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];
if (self)
{
self.strings = strings;
self.fonts = fonts;
self.fontFrames = rects;
self.fontColors = colors;
}
return self;
}
@end
這裡有個細節值得注意:範例中把 [self setNeedsDisplay] 註解掉了。實務上建議在初始化方法中呼叫它,確保視圖首次顯示時會觸發繪製;否則在部分情境下會出現文字沒有畫出來的情況。
直接在需要的地方建立物件並傳入四組陣列即可:
- (void)viewDidLoad
{
[super viewDidLoad];
// 大小寫交錯、字級與顏色皆不同
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];
}
優點是彈性極高:四組陣列各自獨立,想怎麼混排都可以,完全不需要為每個設計稿改寫程式碼。
代價是呼叫端變得冗長,而且矩形位置需要手動計算。如果混排規則是固定的(例如「大數字 + 小單位」),可以在此基礎上再封裝一層語意化的初始化方法,把座標計算收進類別內部,讓呼叫端只傳文字與樣式即可。