纯UILabel实现文字的竖排显示

2023-11-27 12:50

本文主要是介绍纯UILabel实现文字的竖排显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

效果

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

一、计算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的用法以后会介绍。

这篇关于纯UILabel实现文字的竖排显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/427717

相关文章

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方