간단한 HTML 마크 업을로 구문 분석 NSAttributedString
하여 UITextView
. 이 게시물 과 그 게시물을 쉽게 변환 할 수있는 곳을 찾았 습니다. 그것이 내가 사용한 것입니다.
public static NSAttributedString GetAttributedStringFromHtml(string html)
{
NSError error = null;
NSAttributedString attributedString = new NSAttributedString (NSData.FromString(html),
new NSAttributedStringDocumentAttributes{ DocumentType = NSDocumentType.HTML, StringEncoding = NSStringEncoding.UTF8 },
ref error);
return attributedString;
}
이것은 지금까지 작동하지만 이제 기본 글꼴이 매우 작기 때문에 글꼴 크기를 변경하고 싶습니다.
string content = "<strong>I'm strong.</strong><br/>http://www.google.com";
UITextView textView = new UITextView ();
textView.Editable = false;
textView.Font = UIFont.SystemFontOfSize (25);
textView.Text = content;
textView.AttributedText = GetAttributedStringFromHtml (content);
textView.DataDetectorTypes = UIDataDetectorType.Link;
textView.Selectable = true;
위의 코드는 올바르게 구문 분석하지만 글꼴 크기는 변경되지 않습니다. 나는 사용하려고했지만 파싱에 대한 인수로 사용 NSMutableAttributedString
하지 않는 것 같습니다 . 아마도 여러를 결합하는 옵션이 될 수 있지만 방법을 모르겠습니다. 또 다른 옵션은 다음 예제와 같이 캐스트하는 것입니다.NSData
NSAttributedString
NSAttributedString
NSMutableAttributedString attributedString = (NSMutableAttributedString) GetAttributedStringFromHtml (content);
attributedString.AddAttribute (UIStringAttributeKey.Font, UIFont.SystemFontOfSize (25), new NSRange (0, content.Length));
textView.AttributedText = attributedString;
하지만 나는 System.InvalidCastException
.
UITextView
HTML 구문 분석을 사용하더라도 의 글꼴 크기를 어떻게 변경할 수 있습니까?
편집하다:
이제 다음을 만들려고했습니다 NSMutableAttributedString
.
NSAttributedString parsedString = GetAttributedStringFromHtml (content);
NSMutableAttributedString attributedString = new NSMutableAttributedString (parsedString);
attributedString.AddAttribute (UIStringAttributeKey.Font, UIFont.SystemFontOfSize (17), new NSRange (0, attributedString.Length));
textView.AttributedText = attributedString;
이것은 컴파일되고 글꼴 크기가 더 크고 HTML도 구문 분석되지만 <strong>
예를 들어 무시합니다 . 텍스트는 굵게 표시되지 않습니다. 두 번째 속성이 첫 번째 속성을 덮어 쓰는 것 같습니다.
나는 몇 가지를 시도했지만 그들 중 아무것도 작동하지 않았습니다. 그래서 나는 이미 HTML을 파싱하고 있는데 왜 인라인 CSS 구문을 사용하지 않습니까?
<p style='font-size:17px'><strong>I'm bold.</strong><br/>http://www.google.com</p>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다