【问题标题】:Equal spacing between lines of NSAttributedString with multiple font sizes for a UILabel in iOS 6iOS 6中UILabel的多个字体大小的NSAttributedString行之间的间距相等
【发布时间】:2013-06-12 20:35:30
【问题描述】:

我有一个带有属性文本的多行 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];

对我应该使用的代码有什么建议,以便它在每行文本之间完全不显示空格?

【问题讨论】:

    标签: ios fonts uilabel nsattributedstring spacing


    【解决方案1】:

    我一直在做类似的事情,所以也许你可以尝试这样的事情(在浏览器中输入,小心!):

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setAlignment: NSTextAlignmentRight];
    [style setLineSpacing:0];
    
    for(NSString *line in linesArray) {
        NSMutableParagraphStyle *subStyle = [style mutableCopy];
        [subStyle setMaximumLineHeight:10]; // play around with this value <-----
    
        NSDictionary *attributes = 
        @{
          NSFontAttributeName : [UIFont fontWithName:@"TimesNewRomanPSMT" size:fontSize],
          NSParagraphStyleAttributeName : paragraphStyle,
        };
    
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:line attributes: attributes]];
    
        fontSize += 10.0;
    }
    

    【讨论】:

    • 在属性字典中,将“paragraphStyle”更改为“style”对我有用。我有一个稍微不同的情况,即长字符串中的句子需要段落间距而不是数组,因此不需要 for 循环。
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 2023-01-18
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多