纯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中edge-tts实现便捷语音合成

《Python中edge-tts实现便捷语音合成》edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价... 目录安装与环境设置文本转语音查找音色更改语音参数生成音频与字幕总结edge-tts 是一个功能强大的

Java实现按字节长度截取字符串

《Java实现按字节长度截取字符串》在Java中,由于字符串可能包含多字节字符,直接按字节长度截取可能会导致乱码或截取不准确的问题,下面我们就来看看几种按字节长度截取字符串的方法吧... 目录方法一:使用String的getBytes方法方法二:指定字符编码处理方法三:更精确的字符编码处理使用示例注意事项方

使用Python和PaddleOCR实现图文识别的代码和步骤

《使用Python和PaddleOCR实现图文识别的代码和步骤》在当今数字化时代,图文识别技术的应用越来越广泛,如文档数字化、信息提取等,PaddleOCR是百度开源的一款强大的OCR工具包,它集成了... 目录一、引言二、环境准备2.1 安装 python2.2 安装 PaddlePaddle2.3 安装

嵌入式Linux之使用设备树驱动GPIO的实现方式

《嵌入式Linux之使用设备树驱动GPIO的实现方式》:本文主要介绍嵌入式Linux之使用设备树驱动GPIO的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、设备树配置1.1 添加 pinctrl 节点1.2 添加 LED 设备节点二、编写驱动程序2.1

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

spring IOC的理解之原理和实现过程

《springIOC的理解之原理和实现过程》:本文主要介绍springIOC的理解之原理和实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、IoC 核心概念二、核心原理1. 容器架构2. 核心组件3. 工作流程三、关键实现机制1. Bean生命周期2.

Redis实现分布式锁全解析之从原理到实践过程

《Redis实现分布式锁全解析之从原理到实践过程》:本文主要介绍Redis实现分布式锁全解析之从原理到实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景介绍二、解决方案(一)使用 SETNX 命令(二)设置锁的过期时间(三)解决锁的误删问题(四)Re

Java根据IP地址实现归属地获取

《Java根据IP地址实现归属地获取》Ip2region是一个离线IP地址定位库和IP定位数据管理框架,这篇文章主要为大家详细介绍了Java如何使用Ip2region实现根据IP地址获取归属地,感兴趣... 目录一、使用Ip2region离线获取1、Ip2region简介2、导包3、下编程载xdb文件4、J

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整