聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

非换行空格在CoreText排版上的问题

2014-01-24 08:30 浏览: 1607079 次 我要评论(0 条) 字号:

分享一个在使用Core Text时遇到的问题。

问题描述

我们的猿题库界面因为有图片、公式需要混排,所以用的是Core Text实现的排版。

在使用中,发现一些英文题目的换行方式不对, 我们设置的是按单词换行,但是总是有些题目的换行并不是按单词的,造成显示上比较错乱,如下图所示。图中unchangeable, study, falls 三个单词都被生生截断了,造成阅读上的不适。

解决方案

经过我们的分析,我们最终发现这是由于题目内容中有非换行空格non-breaking-space)造成的。根据wikipedia)上的介绍,该字符的ASCII码为160,主要做用如下:

Text-processing software typically assumes that an automatic line break may be inserted anywhere a space character occurs; a non-breaking space prevents this from happening (provided the software recognizes the character). For example, if the text “100 km” will not quite fit at the end of a line, the software may insert a line break between “100” and “km”. To avoid this undesirable behaviour, the editor may choose to use a non-breaking space between “100” and “km”. This guarantees that the text “100 km” will not be broken: if it does not fit at the end of a line it is moved in its entirety to the next line.

于是我们大概知道整个原因了:编辑在后台录入题目时,是在网页做的富文本编辑器里面,里面的空格输入的是  ,然后这个空格转存到数据库中会转成 非换行空格(non-breaking-space),也就是ASCII为160的空格。最后传到iPhone这边,用Core Text排版时,Core Text认为遇到这个空白符不应该换行,于是就一直不换行,直到显示不了了才强制换行,就造成了单词被截断。

解决办法是在客户端上用以下代码将这种空格替换成普通的空格,普通的空格ASCII码为32。(附:ASCII码表):

1
2
3
// 因为显示效果原因,从直观上看不出2个空格在编码上的差异,不过代码复制到Xcode中能看出来。
NSString *content = ...
content = [content stringByReplacingOccurrencesOfString:@" " withString:@" "];

最终改好的效果如下所示:

其它

感谢 @onevcat 提供信息,让我找到问题的根源。



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复