代码之家  ›  专栏  ›  技术社区  ›  devforfu

NSAttribute字符串项目符号列表问题

  •  4
  • devforfu  · 技术社区  · 8 年前

    我正在尝试使用创建项目符号列表 NSAttributedString UITextView . 以下是我迄今为止所取得的成就: enter image description here

    正如人们所看到的,在两条线之间有一个小的“转变”。下面是我用来构建属性字符串的代码片段:

    func add(bulletList strings: [String],
             indentation: CGFloat = 15,
             lineSpacing: CGFloat = 3,
             paragraphSpacing: CGFloat = 10) {
    
        func createParagraphAttirbute() -> NSParagraphStyle {
            var paragraphStyle: NSMutableParagraphStyle
            let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]
    
            paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
            paragraphStyle.tabStops = [
                NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
            paragraphStyle.defaultTabInterval = indentation
            paragraphStyle.firstLineHeadIndent = 0
            paragraphStyle.lineSpacing = lineSpacing
            paragraphStyle.paragraphSpacing = paragraphSpacing
            paragraphStyle.headIndent = indentation
            return paragraphStyle
        }
    
        var buffer = NSMutableAttributedString.init()
    
        for string in strings {
            let formattedString = "\u{2022} \(string)\n"
            let attributedString = NSMutableAttributedString(string: formattedString)
            let paragraphStyle = createParagraphAttirbute()
    
            attributedString.addAttributes(
                [NSAttributedStringKey.paragraphStyle : paragraphStyle],
                range: NSMakeRange(0, attributedString.length))
    
            attributedString.addAttributes(
                textAttributes,
                range: NSMakeRange(0, attributedString.length))
    
            let string:NSString = NSString(string: formattedString)
            let rangeForBullet:NSRange = string.range(of: bulletPoint)
            attributedString.addAttributes(bulletAttirbutes, range: rangeForBullet)
            buffer.append(attributedString)
        }
    }
    

    更新1

    根据@the4kman的建议,我更改了提供的代码,如下所示:

    paragraphStyle.firstLineHeadIndent = indentation
    

    enter image description here

    更新2

    好的,解决方案很简单-用tab替换空格。请参阅下面的更新代码。

    2 回复  |  直到 8 年前
        1
  •  6
  •   devforfu    8 年前

    @这位4kman,@Krunal,谢谢你的回复!解决方案甚至更简单。将空格符号替换为 \t let formattedString = "\u{2022} \(string)\n 提供有效缩进。

    为了完整起见,完整的解决方案代码是(只替换一个字符):

    func add(bulletList strings: [String],
             font: UIFont,
             indentation: CGFloat = 15,
             lineSpacing: CGFloat = 3,
             paragraphSpacing: CGFloat = 10,
             textColor: UIColor = .black,
             bulletColor: UIColor = .red) -> NSAttributedString {
    
        func createParagraphAttirbute() -> NSParagraphStyle {
            var paragraphStyle: NSMutableParagraphStyle
            let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]
    
            paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
            paragraphStyle.tabStops = [
                NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
            paragraphStyle.defaultTabInterval = indentation
            paragraphStyle.firstLineHeadIndent = 0
            paragraphStyle.lineSpacing = lineSpacing
            paragraphStyle.paragraphSpacing = paragraphSpacing
            paragraphStyle.headIndent = indentation
            return paragraphStyle
        }
    
        let bulletPoint = "\u{2022}"
        let textAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: textColor]
        let bulletAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: bulletColor]
        let buffer = NSMutableAttributedString.init()
    
        for string in strings {
            let formattedString = "\(bulletPoint)\t\(string)\n"
            let attributedString = NSMutableAttributedString(string: formattedString)
            let paragraphStyle = createParagraphAttirbute()
    
            attributedString.addAttributes(
                [NSAttributedStringKey.paragraphStyle : paragraphStyle],
                range: NSMakeRange(0, attributedString.length))
    
            attributedString.addAttributes(
                textAttributes,
                range: NSMakeRange(0, attributedString.length))
    
            let string:NSString = NSString(string: formattedString)
            let rangeForBullet:NSRange = string.range(of: bulletPoint)
            attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
            buffer.append(attributedString)
        }
    
        return buffer
    }
    
        2
  •  0
  •   zeroimpl    7 年前

    下面是一个简单的Objective-C代码片段,主要基于公认的答案:

    NSString* text = 
                "•\tSome text for bullet 1.\n"
                "•\tSome text for bullet 2.\n"
                "•\tSome text for bullet 3."
    
    UIFont* bodyFont = [UIFont preferredFontForTextStyle: UIFontTextStyleBody];
    CGFloat indentSize = bodyFont.pointSize;
    NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init];
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.tabStops = @[ [[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:indentSize options:@{}] ];
    paraStyle.defaultTabInterval = indentSize;
    paraStyle.headIndent = indentSize;
    paraStyle.firstLineHeadIndent = 0;
    paraStyle.lineHeightMultiple = 0.85;
    paraStyle.lineSpacing = 0;
    paraStyle.paragraphSpacing = bodyFont.lineHeight * 0.25;
    paraStyle.paragraphSpacingBefore = 0;
    [atrStr appendAttributedString:
     [[[NSMutableAttributedString alloc] initWithString:avc.message attributes:@{
          NSParagraphStyleAttributeName:paraStyle,
          NSFontAttributeName: bodyFont,
      }]];