2017-04-24 8 views
0

안녕하세요 저는 NSMutableAttributedString에 htmlString 변환하고 객관적인 C. 새로운 오전. 하지만 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

은 **이 **, 당신은 할 수 없습니다. '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