纯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

相关文章

Java实现将Markdown转换为纯文本

《Java实现将Markdown转换为纯文本》这篇文章主要为大家详细介绍了两种在Java中实现Markdown转纯文本的主流方法,文中的示例代码讲解详细,大家可以根据需求选择适合的方案... 目录方法一:使用正则表达式(轻量级方案)方法二:使用 Flexmark-Java 库(专业方案)1. 添加依赖(Ma

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Mybatis从3.4.0版本到3.5.7版本的迭代方法实现

《Mybatis从3.4.0版本到3.5.7版本的迭代方法实现》本文主要介绍了Mybatis从3.4.0版本到3.5.7版本的迭代方法实现,包括主要的功能增强、不兼容的更改和修复的错误,具有一定的参考... 目录一、3.4.01、主要的功能增强2、selectCursor example3、不兼容的更改二、

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

Flask 验证码自动生成的实现示例

《Flask验证码自动生成的实现示例》本文主要介绍了Flask验证码自动生成的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录生成图片以及结果处理验证码蓝图html页面展示想必验证码大家都有所了解,但是可以自己定义图片验证码

VSCode配置Anaconda Python环境的实现

《VSCode配置AnacondaPython环境的实现》VisualStudioCode中可以使用Anaconda环境进行Python开发,本文主要介绍了VSCode配置AnacondaPytho... 目录前言一、安装 Visual Studio Code 和 Anaconda二、创建或激活 conda

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

JAVA封装多线程实现的方式及原理

《JAVA封装多线程实现的方式及原理》:本文主要介绍Java中封装多线程的原理和常见方式,通过封装可以简化多线程的使用,提高安全性,并增强代码的可维护性和可扩展性,需要的朋友可以参考下... 目录前言一、封装的目标二、常见的封装方式及原理总结前言在 Java 中,封装多线程的原理主要围绕着将多线程相关的操