UITextView中的汉字行间距

时间:2015-07-02 22:02:47

标签: ios objective-c uitextview

我已将应用翻译成简体中文。 该应用程序使用的是UITextView,其内容是简体中文文本。

我注意到(有时并不总是)行间距是错误的,行几乎是相互接触的:

enter image description here

我正在通过我根据所需的点数创建的属性字符串设置UITextview的内容:

-(NSMutableAttributedString*) textWithPointSize:(CGFloat)pointSize
{
 UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
 NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:@{NSFontAttributeName:myFontDescr}];
 UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
 NSMutableAttributedString * tips        = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips  attributes:@{NSFontAttributeName:myFontTips}];

 NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
 [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
 [text appendAttributedString:tips];

 return text;
}

我正在使用systemFontOfSize:函数,我想这应该正确处理中文字符?任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

我没有找到原因,但我通过明确设置行间距(仅当语言是中文)来实现一个解决方法,如下所示:

  //for chinese language we have detected a problem with the interline spacing
    //it does happen randomly, and so to workaround this we set the linespacing explicitly
    if ( [NSLocalizedString(@"__CurrentLanguage",@"zh-Hans") isEqualToString:@"zh-Hans"] ) {
        //make paragraph styl with interline spacing
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 0.25 * pointSize;
        //font for description
        UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
        NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:@{NSFontAttributeName:myFontDescr, NSParagraphStyleAttributeName:paragraphStyle}];
        //font for tips
        UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
        NSMutableAttributedString * tips        = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips  attributes:@{NSFontAttributeName:myFontTips,NSParagraphStyleAttributeName:paragraphStyle}];
        //add description then...
        NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
        //concat the tips (with 2x newline in between)
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
        [text appendAttributedString:tips];
        //return value
        return text;
    }
    //its not chinese, so do "normal" stuff