【问题标题】:Setting font on NSAttributedString on UITextView disregards line spacing在 UITextView 上的 NSAttributedString 上设置字体忽略行距
【发布时间】:2018-07-17 16:41:14
【问题描述】:

我正在尝试将属性字符串设置为 iOS 6 中的 UITextView。问题是,如果我尝试在属性字符串上设置字体属性,则会忽略行间距。但是,如果我不设置字体,而使用默认字体,则行距有效。

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

paragraphStyle.minimumLineHeight = 50;
// setting the font below makes line spacing become ignored
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

mainTextView.attributedText = attrString;

知道发生了什么吗?

【问题讨论】:

  • 您打算接受答案还是没有答案对您有帮助? :)
  • 是的,我想是的。请参阅下面的答案:)
  • 嘿,如果你接受了一个答案,如果有一个对你有帮助的话,那还是很酷的。

标签: objective-c ios6


【解决方案1】:

Attributed String Programming Guide:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];

更新:我尝试在自己的应用程序中使用 addAttribute: 方法,但它似乎不适用于 iOS 6 模拟器:

NSLog(@"%@", textView.attributedText);

日志似乎显示正确添加的属性,但 iOS 模拟器上的视图未显示属性。

【讨论】:

  • 我在尝试添加属性时遇到了同样的问题。我试过你的答案,它甚至没有用。有什么建议吗??如果我们在谈论 IOS,我认为你应该编辑你对 UIFont 而不是 NSFont 的答案。
  • 我不知道为什么人们会投票赞成这个答案。它不能解决实际问题。
  • 这个答案有点老所以使用: UIFont * font = [UIFont fontWithName: @"Palatino-Roman" size:14.0] 修复
  • 当然还是不行。似乎添加,甚至设置属性对属性字符串没有影响。
  • 很抱歉,但这仍然不是答案。
【解决方案2】:

我发现了你的问题,因为我也在和 NSAttributedString 打架。 对我来说,beginEditingendEditing 方法可以解决问题,就像 Changing an Attributed String 中所述。 除此之外,lineSpacing 在段落样式中设置为setLineSpacing

因此您可能想尝试将代码更改为:

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

[paragraphStyle setLineSpacing:20]  // Or whatever (positive) value you like...    
[attrSting beginEditing];

[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

[attrString endEditing];

mainTextView.attributedText = attrString;

虽然没有测试这个确切的代码,顺便说一句,但我的看起来几乎一样。

编辑:

同时,我已经对其进行了测试,如果我错了,请纠正我,- beginEditing- endEditing 调用似乎非常重要。

【讨论】:

    【解决方案3】:
    //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];
    

    【讨论】:

      【解决方案4】:

      iOS 6 中存在一个错误,导致在设置字体时忽略行高。请参阅NSParagraphStyle line spacing ignored 的答复和Radar: UITextView Ignores Minimum/Maximum Line Height in Attributed String 的更长错误分析。

      【讨论】:

        【解决方案5】:

        您可以使用this example 并像这样更改它的实现:

        [self enumerateAttribute:NSParagraphStyleAttributeName
                         inRange:NSMakeRange(0, self.length)
                         options:0
                      usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
                          NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        
                          //add your specific settings for paragraph
                          //...
                          //...
        
                          [self removeAttribute:NSParagraphStyleAttributeName range:range];
                          [self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
                      }];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-20
          • 2011-07-29
          • 1970-01-01
          • 1970-01-01
          • 2011-11-12
          • 1970-01-01
          • 1970-01-01
          • 2012-02-08
          相关资源
          最近更新 更多