iOS 6 多行标签行距 [英] iOS 6 multiline label line spacing

查看:37
本文介绍了iOS 6 多行标签行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UILabel 中的行距有问题,我使用的是自定义字体,当我使用表情符号时,两行之间没有空格.这显然看起来不太好.所以我使用此代码来设置行间距,但应用程序崩溃并给出错误

<块引用><块引用>

由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:NSAttributedString 对自动调整无效,它必须具有单跨段落样式(或无)和非换行 lineBreakMode."

if ([cell.label2 RespondsToSelector:@selector(setAttributedText:)]){UIFont *font =btMyriadProRegularWithSize14Pt;NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];[paragraphStyle setLineSpacing: 22];NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName:paragraphStyle };NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:chatMessage.msgString attributes:attributes];[cell.label2 setAttributedText:attributedString];}别的{NSString * msg = [NSString stringWithFormat:@"%@: %@",chatMessage.from,chatMessage.msgString];cell.label2.text = msg;}

解决方案

试试这个

 [cell.label2 setAdjustsFontSizeToFitWidth:NO];

甚至可能只适用于 iOS6

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {//*[cell.label2 setAdjustsFontSizeToFitWidth:NO];}

There is a problem with line spacing in UILabel, I am using custom font and when I use smilies there is no space between two lines. which obviously looks not so good. So I used this code for line spacing but app crashes giving the error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSAttributedString invalid for autoresizing, it must have a single spanning paragraph style (or none) with a non-wrapping lineBreakMode.'

if ([cell.label2 respondsToSelector:@selector(setAttributedText:)])
    {
        UIFont *font =btMyriadProRegularWithSize14Pt;

        NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [paragraphStyle setLineSpacing: 22];

        NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle };
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:chatMessage.msgString attributes:attributes];

        [cell.label2 setAttributedText: attributedString];
    }
    else
    {
        NSString * msg = [NSString stringWithFormat:@"%@: %@",chatMessage.from,chatMessage.msgString];
        cell.label2.text = msg;
    }

解决方案

try this

    [cell.label2 setAdjustsFontSizeToFitWidth:NO];

maybe even only for iOS6

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //*
       [cell.label2 setAdjustsFontSizeToFitWidth:NO];
}

这篇关于iOS 6 多行标签行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆