纯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

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被