iOS 6中UILabel具有多个字体大小的NSAttributedString行之间的相等间距

iOS 6中UILabel具有多个字体大小的NSAttributedString行之间的相等间距,ios,fonts,uilabel,nsattributedstring,spacing,Ios,Fonts,Uilabel,Nsattributedstring,Spacing,我有一个带有属性文本的多行UILabel 文本中的所有行的字体相同,但每行的字体大小不同 CGFloat y = 0; NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@""]; NSArray *linesArray = [NSArray arrayWithObjects:@"One I\n", @"Two I\n",

我有一个带有属性文本的多行UILabel

文本中的所有行的字体相同,但每行的字体大小不同

CGFloat y = 0;

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@""];
NSArray *linesArray = [NSArray arrayWithObjects:@"One I\n",
                       @"Two I\n",
                       @"Three I\n",
                       @"Four I\n",
                       @"Five I\n", nil];

CGFloat fontSize = 10.0;

for(NSString *line in linesArray) {

    NSMutableAttributedString *attributedLine = [[NSMutableAttributedString alloc] initWithString:line];
    NSInteger stringLength=[line length];
    [attributedLine addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"TimesNewRomanPSMT" size:fontSize]
                              range:NSMakeRange(0, stringLength)];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 0.0f;
    paragraphStyle.alignment = NSTextAlignmentRight;
    [attributedLine addAttributes:@{ NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, stringLength)];


    [attString appendAttributedString:attributedLine];

    fontSize += 10.0;
}

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
label.attributedText = attString;
[label sizeToFit];
CGRect newFrame = label.frame;
newFrame.size.width = self.view.frame.size.width - 40;
newFrame.origin.y = y;
newFrame.origin.x = 0;
label.frame = newFrame;
[self.view addSubview:label];
我试图在每条线之间获得完全相同的垂直空间

但是,显示的内容具有可变空间。就好像有什么东西在根据字体大小为字体添加垂直边距

CGFloat y = 0;

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@""];
NSArray *linesArray = [NSArray arrayWithObjects:@"One I\n",
                       @"Two I\n",
                       @"Three I\n",
                       @"Four I\n",
                       @"Five I\n", nil];

CGFloat fontSize = 10.0;

for(NSString *line in linesArray) {

    NSMutableAttributedString *attributedLine = [[NSMutableAttributedString alloc] initWithString:line];
    NSInteger stringLength=[line length];
    [attributedLine addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"TimesNewRomanPSMT" size:fontSize]
                              range:NSMakeRange(0, stringLength)];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 0.0f;
    paragraphStyle.alignment = NSTextAlignmentRight;
    [attributedLine addAttributes:@{ NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, stringLength)];


    [attString appendAttributedString:attributedLine];

    fontSize += 10.0;
}

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
label.attributedText = attString;
[label sizeToFit];
CGRect newFrame = label.frame;
newFrame.size.width = self.view.frame.size.width - 40;
newFrame.origin.y = y;
newFrame.origin.x = 0;
label.frame = newFrame;
[self.view addSubview:label];

为了使代码在每行文本之间不显示任何空格,我应该使用哪些代码?我一直在做类似的事情,所以您可以尝试类似的方法(在浏览器中键入,小心!):

NSMutableParagraphStyle*style=[[nsmutableparaphstyle alloc]init];
[style setAlignment:NSTextAlignmentRight];
[样式设置行距:0];
用于(NSString*行中的行RAY){
NSMutableParagraphStyle*subStyle=[style mutableCopy];

[subStyle setMaximumLineHeight:10];//使用此值玩游戏我一直在做类似的操作,所以您可以尝试类似的操作(在浏览器中键入,小心!)

NSMutableParagraphStyle*style=[[nsmutableparaphstyle alloc]init];
[style setAlignment:NSTextAlignmentRight];
[样式设置行距:0];
用于(NSString*行中的行RAY){
NSMutableParagraphStyle*subStyle=[style mutableCopy];

[subStyle setMaximumLineHeight:10];//在属性字典中使用此值,将“paragraphStyle”更改为“style”对我来说很有效。我有一个稍微不同的情况,即在长字符串中的句子上需要段落间距,而不是在数组中,因此不需要for循环。在属性字典中,更改“paragraphStyle”“风格”对我来说很有效。我有一个稍微不同的情况,在一个长字符串中的句子上需要段落间距,而不是在一个数组中,所以不需要for循环。