Setting font on NSAttributedString on UITextView disregards line spacing

后端 未结 5 1278
执笔经年
执笔经年 2020-12-08 03:35

I\'m trying to set an attributed string to a UITextView in iOS 6. The problem is, if I attempt to set the font property on the attributed string, the line spacing is ignored

5条回答
  •  一整个雨季
    2020-12-08 04:23

    //For proper line spacing
    
    NSString *text1 = @"Hello";
    NSString *text2 = @"\nWorld";
    UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
    NSMutableAttributedString *attributedString1 =
    [[NSMutableAttributedString alloc] initWithString:text1 attributes:@{ NSFontAttributeName : text1Font }];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setAlignment:NSTextAlignmentCenter];
    [paragraphStyle1 setLineSpacing:4];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];
    
    UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
    NSMutableAttributedString *attributedString2 =
    [[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
    NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle2 setLineSpacing:4];
    [paragraphStyle2 setAlignment:NSTextAlignmentCenter];
    [attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];
    
    [attributedString1 appendAttributedString:attributedString2];
    

提交回复
热议问题