20

私はiOS 7専用のアプリを書いています.編集不可能なUITextViewの箇条書きで適切なフォーマットを取得しようとしています.

箇条書き文字を挿入するだけで十分簡単ですが、当然、左インデントは続きません。iOS 7 で箇条書きの後に左インデントを設定する最も簡単な方法は何ですか?

前もって感謝します、

フランク

4

8 に答える 8

8

これは私が見つけた最も簡単な解決策です:

let bulletList = UILabel()
let bulletListArray = ["line 1 - enter a bunch of lorem ipsum here so it wraps to the next line", "line 2", "line 3"]
let joiner = "\n"

var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 10
paragraphStyle.firstLineHeadIndent = 0

let attributes = [NSParagraphStyleAttributeName: paragraphStyle]
let bulletListString = joiner.join(bulletListArray.map { "• \($0)" })

bulletList.attributedText = NSAttributedString(string: bulletListString, attributes: attributes)

配列内の各文字列であるという理論は「段落」のように機能し、段落スタイルは最初の行で 0 インデントを取得し、map メソッドを使用して箇条書きが追加されます..その後、すべての行で 10 px のインデントが取得されます (間隔を調整します)フォント メトリクス用)

于 2014-11-14T20:01:49.987 に答える
6

他の答えは、インデントサイズを定数値で設定することに依存しています。つまり、フォントを変更する場合は手動で更新する必要があり、Dynamic Type を使用している場合はうまく機能しません。幸いなことに、テキストの測定は簡単です。

いくつかのテキストといくつかの属性があるとしましょう:

NSString *text = @"• Some bulleted paragraph";
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
NSDictionary *attributes = @{NSFontAttributeName: font};

箇条書きを測定し、それに応じて段落スタイルを作成する方法は次のとおりです。

NSString *bulletPrefix = @"• ";
CGSize size = [bulletPrefix sizeWithAttributes:attributes];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.headIndent = size.width;

これを属性に挿入し、属性付きの文字列を作成します。

NSMutableDictionary *indentedAttributes = [attributes mutableCopy];
indentedAttributes[NSParagraphStyleAttributeName] = [paragraphStyle copy];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:indentedAttributes];
于 2016-05-06T08:35:25.690 に答える
5

スイフト4

NSAttributedStringさまざまな種類のリストを適切にインデントする便利な初期化子を追加するための拡張機能を作成しました。

extension NSAttributedString {

    convenience init(listString string: String, withFont font: UIFont) {
        self.init(attributedListString: NSAttributedString(string: string), withFont: font)
    }

    convenience init(attributedListString attributedString: NSAttributedString, withFont font: UIFont) {
        guard let regex = try? NSRegularExpression(pattern: "^(\\d+\\.|[•\\-\\*])(\\s+).+$",
                                                   options: [.anchorsMatchLines]) else { fatalError() }
        let matches = regex.matches(in: attributedString.string, options: [],
                                    range: NSRange(location: 0, length: attributedString.string.utf16.count))
        let nsString = attributedString.string as NSString
        let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)

        for match in matches {
            let size = NSAttributedString(
                string: nsString.substring(with: match.range(at: 1)) + nsString.substring(with: match.range(at: 2)),
                attributes: [.font: font]).size()
            let indentation = ceil(size.width)
            let range = match.range(at: 0)

            let paragraphStyle = NSMutableParagraphStyle()

            if let style = attributedString.attribute(.paragraphStyle, at: 0, longestEffectiveRange: nil, in: range)
                as? NSParagraphStyle {
                paragraphStyle.setParagraphStyle(style)
            }

            paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: indentation, options: [:])]
            paragraphStyle.defaultTabInterval = indentation
            paragraphStyle.firstLineHeadIndent = 0
            paragraphStyle.headIndent = indentation

            mutableAttributedString.addAttribute(.font, value: font, range: range)
            mutableAttributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
        }

        self.init(attributedString: mutableAttributedString)
    }
}

使用例: 便利な初期化子の使用方法

各箇条書きの後のスペースの数などは問題ではありません。コードは、箇条書きの後に配置するタブまたはスペースの数に基づいて、適切なインデント幅を動的に計算します。

属性付き文字列に既に段落スタイルがある場合、便利な初期化子はその段落スタイルのオプションを保持し、独自のオプションをいくつか適用します。

サポートされている記号: •、-、*、ピリオドが続く数字 (例: 8.)

于 2017-09-21T07:50:54.913 に答える