2017-04-24 13 views
0

こんにちは私は目的C.に新しいです。私はNSMutableAttributedStringにhtmlStringを変換しています。しかし、NSMutableAttributedStringのフォントサイズを変更すると、テキストから太字のフォントが削除されてしまいます。太字の書式を削除せずにNSMutableAttributedStringのフォントサイズを変更します。

マイコード:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil ]; 



     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 
//   overView.font = [UIFont systemFontOfSize:16]; 
      [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, attributedString.length)]; 
     } 
     else{ 
//   overView.font = [UIFont systemFontOfSize:12]; 
      [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, attributedString.length)]; 
     } 
     overView.attributedText = (NSAttributedString *)attributedString; 

すべてのヘルプは、おかげでいただければ幸いです。

+0

Not ** it **、しています。 'boldSystemFontOfSize'メソッドもあります。 – vadian

+0

@vadianそれはすべての文字列を太字にします。 – Furqan

+0

もちろん、範囲は文字列全体に指定されているためです。部分的な文字列が必要な場合は、範囲を調整する必要があります。 – vadian

答えて

0

これを試してください。変更された文字列を表示するには、copy stringを使用する必要があります。

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil ]; 
    NSMutableAttributedString* copy = [attributedString mutableCopy]; 
    [attributedString enumerateAttributesInRange:NSMakeRange(0, string.length) options:0 usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) { 
    UIFont* font = attrs[NSFontAttributeName]; 
    if(font) 
    { 
     NSMutableDictionary* changedAttributes = [attrs mutableCopy]; 
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 
     changedAttributes[NSFontAttributeName] = [UIFont fontWithName:font.fontName size:16]; 
     } 
     else{ 
     changedAttributes[NSFontAttributeName] = [UIFont fontWithName:font.fontName size:12]; 
     } 
     [copy setAttributes:changedAttributes range:range]; 
    } 
    }]; 
+0

これは機能しません。 – Furqan

+0

文字列を表示するには**コピー**を使用しますか?このオブジェクトのすべての変更は、attributedStringではなく、変更されています。 – Sergey

+0

overView.attributedText =(NSAttributedString *)copy; 私はこれをやっています。動作しません。 – Furqan

関連する問題