纯UILabel实现文字的竖排显示

时间:2023-02-11 07:43:47

做iOS开发要经常用到UILabel。UILabel一般只适用一些简单的文字显示。当需要复杂的图文混排的时候,可以用CoreText来做。但是CoreText用法复杂,当我们遇到一些不是很复杂的交互需求时,用CoreText就有点杀鸡用牛刀的意思。这里介绍一下我在项目里遇到的一个案例。
先来看看效果:

纯UILabel实现文字的竖排显示

这里要求文字都是竖排显示,并且当文字是非汉字的时候又要把文字侧过来以方便阅读。
下面就介绍一下我实现这个需求的过程。

一、计算label的高度

在很多场景下,我们需要事先知道这个label的size,下面这个方法可以计算在已知width、font和label中的text内容的情况下,这个label的高度。

1
2
3
4
5
6
- (CGSize)getSize:(NSString *)detail font:(UIFont *)font width:( float )width
{
     CGSize constraint = CGSizeMake(width, 20000 .0f);
     CGSize size = [detail sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];
     return size;
}

利用这个方法也可以简单的实现竖排显示文字,只需要把width参数设置成一个字符的宽度就可以。一般字体大小在14 – 20之间,width参数设置为14.0

二、如何让label置顶或其它

label默认的textAlignment只有UITextAlignmentLeft、UITextAlignmentCenter、UITextAlignmentRight三种。
在需要文字居顶,居底时,UILabel的api就不够用了。不过这个时候我们可以通过重写UILabel的- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines方法来实现。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
     CGRect textRect = [ super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
     switch (self.verticalAlignment) {
         case VerticalAlignmentTop:
             textRect.origin.y = bounds.origin.y;
             break ;
         case VerticalAlignmentBottom:
             textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
             break ;
         case VerticalAlignmentMiddle:
             // Fall through.
         default :
             textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0 ;
     }
     return textRect;
}
 
// 然后在drawTextInRect:方法中使用重新计算的CGRect
 
-( void )drawTextInRect:(CGRect)requestedRect
{
     CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
     [ super drawTextInRect:actualRect];
}

三、竖排显示文字时,如何让汉字竖排,而英文和数字为横排

汉字竖排显示没有压力,但是如果英文也都竖排显示就很难读懂了。
我这里的做法是:
1、拆分label的内容,把汉字、英文拆分开,并放在多个label中显示。
2、英文横排显示,然后把label旋转90度。

代码如下:
a) 拆分内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
*  拆分标题中的中文与非中文
*/
- (NSArray *)splitTitle:(NSString *)title
{
     NSArray *stringItemArray = [[NSMutableArray alloc] init];
     int type = 0 ;
     if ([self isChinese:title index: 0 ]) {
         type = 1 ;
     } else {
         type = 0 ;
     }
     int fromIndex = 0 ;
     int toIndex = 0 ;
     for ( int i = 1 ; i < title.length; i++) {
         if (type == 1 ) {
             if ([self isChinese:title index:i]) {
                 if (i == title.length - 1 ) {
                     StringItem *item = [[StringItem alloc] init];
                     item.isChinese = [self isChinese:title index:i];
                     item.content = [title substringWithRange:NSMakeRange(fromIndex, title.length - fromIndex)];
                     [stringItemArray addObject:item];
                     [item release];
                 }
                 toIndex = i;
             } else {
                 type = 0 ;
                 StringItem *item = [[StringItem alloc] init];
                 item.isChinese = YES;
                 item.content = [title substringWithRange:NSMakeRange(fromIndex, toIndex - fromIndex + 1 )];
                 [stringItemArray addObject:item];
                 [item release];
                 
                 fromIndex = i;
                 toIndex = i;
                 if (i == title.length - 1 ) {
                     StringItem *item = [[StringItem alloc] init];
                     item.isChinese = [self isChinese:title index:i];
                     item.content = [title substringWithRange:NSMakeRange(fromIndex, title.length - fromIndex)];
                     [stringItemArray addObject:item];
                     [item release];
                     
                 }
             }
         } else {
             if (![self isChinese:title index:i]) {
                 if (i == title.length - 1 ) {
                     StringItem *item = [[StringItem alloc] init];
                     item.isChinese = [self isChinese:title index:i];
                     item.content = [title substringWithRange:NSMakeRange(fromIndex, title.length - fromIndex)];
                     [stringItemArray addObject:item];
                     [item release];
                     
                 }
                 toIndex = i;
             } else {
                 type = 1 ;
                 StringItem *item = [[StringItem alloc] init];
                 item.isChinese = NO;
                 item.content = [title substringWithRange:NSMakeRange(fromIndex, toIndex - fromIndex + 1 )];
                 [stringItemArray addObject:item];
                 [item release];
                 
                 fromIndex = i;
                 toIndex = i;
                 if (i == title.length - 1 ) {
                     StringItem *item = [[StringItem alloc] init];
                     item.isChinese = [self isChinese:title index:i];
                     item.content = [title substringWithRange:NSMakeRange(fromIndex, title.length - fromIndex)];
                     [stringItemArray addObject:item];
                     [item release];
                 }
             }
         }
     }
     return stringItemArray;
}
  //判断是否是中文
- (BOOL)isChinese:(NSString *)str index:( int )index
{
     NSRange range = NSMakeRange(index, 1 );
     NSString *subString = [str substringWithRange:range];
     const char *cString = [subString UTF8String];
     if (strlen(cString) == 3 ) {
         return YES;
     } else {
         return NO;
     }
}

b) 处理多个label,非中文的label旋转90度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
CGRect frame = self.frame;
UIFont *font = [UIFont systemFontOfSize: 16 ];
float height = 40 ;
for (StringItem *item in stringItemArray) {
     if (item.isChinese) {
         CGSize size = [self getSize:item.content font:font];
         VerticallyAlignedLabel *label = [[VerticallyAlignedLabel alloc] initWithFrame:CGRectMake((frame.size.width - size.width) / 2 , height, size.width, size.height)];
         label.verticalAlignment = VerticalAlignmentTop;
         label.textAlignment = UITextAlignmentCenter;
         label.textColor = textColor;
         label.font = font;
         label.backgroundColor = [UIColor clearColor];
         [label setLineBreakMode:UILineBreakModeWordWrap];
         label.text = item.content;
         
         label.numberOfLines = 0 ;
         
         [self addSubview:label];
         height = height + label.frame.size.height;
     } else {
         CGSize size = [self getVerSize:item.content font:font];
         VerticallyAlignedLabel *label = [[VerticallyAlignedLabel alloc] initWithFrame:CGRectMake((frame.size.width - size.height) / 2 , height, size.height > frame.size.height - height ? frame.size.height - height : size.width, size.height)];
         label.verticalAlignment = VerticalAlignmentTop;
         label.textAlignment = UITextAlignmentCenter;
         label.textColor = textColor;
         label.font = font;
         label.backgroundColor = [UIColor clearColor];
         [label setLineBreakMode:UILineBreakModeWordWrap];
         label.text = item.content;
         
         label.numberOfLines = 1 ;
         
         label.transform = CGAffineTransformMakeRotation(M_PI_2);
         
         label.frame = CGRectMake((frame.size.width - label.frame.size.width) / 2 , height, label.frame.size.width, label.frame.size.height);
         [self addSubview:label];
         height = height + label.frame.size.height;
     }
}

这样我们就可以用一些简单的api去实现优雅的竖排文字显示。而不需要复杂的CoreText去实现了。
但是如果是复杂的图文混排,最好还是用CoreText去实现。CoreText的用法以后会介绍。