UILabel - attributedText - setLineHeightMultiple

时间:2015-02-24 17:29:37

标签: ios objective-c

我正在使用UILabel来显示文字。

问题是,我在不同的IOS版本之间有不同的行间距。 IOS7.1的空间小于IOS8.1

修复它的最佳方法是什么?我没有安装所有的模拟器,所以当问题出现时我无法弄清楚。有没有人有办法解决吗?这是当前的代码:

    NSString *startGameBasic_title = @"TRACK QUIZ"; // FONT SIZE 20 px
    NSString *startGameBasic_1 = @"BASIC"; // FONT SIZE 76 px
    NSString *startGameBasic_2 = @"MODE"; // FONT SIZE 76 px

    NSString *startGameBasic_textRe = [NSString stringWithFormat:@"%@\n\n%@\n%@",
                        startGameBasic_title,
                        startGameBasic_1,
                        startGameBasic_2];


    // Define general attributes like color and fonts for the entire text
    NSDictionary *startGameBasic_attribsRe = @{
                                NSForegroundColorAttributeName: GENERAL_BACK_BUTTON_COLOR,
                                NSFontAttributeName:START_VIEW_FONT_SMALL,
                                NSKernAttributeName:@(0.0f)
                                };
    NSMutableAttributedString *startGameBasic_attributedTextRe = [[NSMutableAttributedString alloc] initWithString:startGameBasic_textRe attributes:startGameBasic_attribsRe];

    //TITEL
    NSRange startGameBasic_text_title = [startGameBasic_textRe rangeOfString:startGameBasic_title];
    [startGameBasic_attributedTextRe setAttributes:@{NSForegroundColorAttributeName:GENERAL_BACK_BUTTON_COLOR,
                                      NSFontAttributeName:START_VIEW_FONT_SMALL,
                                      NSKernAttributeName:@(-0.5f)}
                              range:startGameBasic_text_title];
    //FIRSTROW
    NSRange startGameBasic_text_1 = [startGameBasic_textRe rangeOfString:startGameBasic_1];
    [startGameBasic_attributedTextRe setAttributes:@{NSForegroundColorAttributeName:GENERAL_BACK_BUTTON_COLOR,
                                      NSFontAttributeName:START_BASIC_BUTTON,
                                      NSKernAttributeName:@(-3.5f)}
                              range:startGameBasic_text_1];
    //SECONDROW
    NSRange buttonReStart_text_2 = [startGameBasic_textRe rangeOfString:startGameBasic_2];
    [startGameBasic_attributedTextRe setAttributes:@{NSForegroundColorAttributeName:GENERAL_BACK_BUTTON_COLOR,
                                      NSFontAttributeName:START_BASIC_BUTTON,
                                      NSKernAttributeName:@(-2.8f)}
                              range:buttonReStart_text_2];


    // Create NSMutableParagraphStyle object
    NSMutableParagraphStyle *startGameBasic_paragraphRe = [[NSMutableParagraphStyle alloc] init];
    startGameBasic_paragraphRe.alignment = NSTextAlignmentCenter;
   [startGameBasic_paragraphRe setLineHeightMultiple:0.65];

[startGameBasic_attributedTextRe addAttribute:NSParagraphStyleAttributeName value:startGameBasic_paragraphRe range:NSMakeRange(0, [startGameBasic_attributedTextRe length])];

    startGameBasic_txt.attributedText = startGameBasic_attributedTextRe;

现在,我有这样做的想法,但也许有更好的方法:

NSMutableParagraphStyle *startGameBasic_paragraphRe = [[NSMutableParagraphStyle alloc] init];
startGameBasic_paragraphRe.alignment = NSTextAlignmentCenter;

float SystemVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (SystemVersion < 8.0)
{
[startGameBasic_paragraphRe setLineHeightMultiple:0.85];
} else {
[startGameBasic_paragraphRe setLineHeightMultiple:0.65];
}

我认为使用UITextView不是解决方案,因为我无法设置“缩放字体大小”。很高兴得到你的帮助。

0 个答案:

没有答案