7

文字列がある場合は、次のように言います。

これは < b >単純な < /b > 文字列です。

< b > を取り除く必要があります (申し訳ありませんが、b と山かっこの間にスペースがありません。何らかの理由でプレビューに表示されません)。また、「シンプル」という単語を太字にする必要があります。私の考えは次のとおりです。

  1. 山かっこと br を空のスペースに置き換えます
  2. 「単純な」セグメントに属性を持たせる

問題は、タグが削除されたら、単語の場所を知る必要があることです。最初に「シンプル」の場所を覚えていますか?削除後、場所-4 は「シンプル」の新しい場所になるはずです? もっと良い方法はありますか?それとも、html タグを属性に変換しますか?

ありがとう

編集: br ではなく b にする必要があります

4

4 に答える 4

4

現在の回答は問題ありません。+1 しました。しかし、それはヒントに過ぎず、本当の解決策ではありませんでした。OPの質問に対する解決策を探している場合は、こちらをご覧ください。

次の点に注意する必要があります。

最初にこれらのメソッドを実装します。

- (NSString *)styledHTMLwithHTML:(NSString *)HTML {
    NSString *style = @"<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>";

    return [NSString stringWithFormat:@"%@%@", style, HTML];
}

- (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML {
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
    return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL];
}

後でそのように使用します:

// This is a string that you might find in your model
NSString *html = @"This is <b>bold</b>";

// Apply some inline CSS
NSString *styledHtml = [self styledHTMLwithHTML:html];

// Generate an attributed string from the HTML
NSAttributedString *attributedText = [self attributedStringWithHTML:styledHtml];

// Set the attributedText property of the UILabel
label.attributedText = attributedText;
于 2016-05-24T00:13:10.677 に答える