iPhone 개발 - Uiwebview 글꼴 설정
문제
서버에서 가져 오는 풍부한 텍스트를 보여 주어야하므로 uiwebview를 사용하고 있습니다. 이제 문제는 Uiwebview에서 사용되는 글꼴을 제어 할 수 없다는 것입니다. 시스템 글꼴을 사용하기 위해 글꼴을 변경하려면 어떻게해야합니까 (응용 프로그램의 나머지와 일치하도록)?
나는 지금 이런 일을하고있다 :
myRichTextView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, width, height)];
myRichTextView.backgroundColor = [UIColor clearColor];
[myRichTextView setScalesPageToFit:NO];
[myRichTextView loadHTMLString:myString baseURL:[NSURL URLWithString:myBaseURL]];
다시, 디스플레이 글꼴을 제어해야합니다. 글꼴을 제어 할 수 없다면 Uiwebview에 대한 다른 대안을 알려주십시오.
감사
해결책
HTML 문서에서 CSS 스타일 시트를 참조하거나 스타일 정보를 HTML 문서 자체의 상단에 넣으십시오.
다른 팁
이와 같이 문자열을 수정했을 때 작동했습니다. 당신의 오른쪽 크리스, 그것은 아무런 차이를 만들지 않아야합니다.
NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%@\"; font-size: %@;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", @"helvetica", [NSNumber numberWithInt:kFieldFontSize], content];
웹 뷰에서 다음 CSS를 사용하여 정확히 귀하가 말하는 것을 수행합니다.
body
{
font-family:helvetica;
}
그러나 당신은 그것이 효과가있는 것처럼 보입니다. 이상한. 내가 볼 수있는 유일한 차이점은 글꼴 대 글꼴 가족이지만 그렇지 않아야합니다 차이를 만들다.
크리스.
시스템 글꼴
-응용 프로그램 iOS 9 & Safari OS X 10
font-family: -apple-system;
font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible
나는 방법을 만들었다 그것은 주어진에 대한 올바른 스타일로 HTML 본체에 텍스트를 감아줍니다. UIColor
그리고 UIFont
. 그것은 기반입니다 무스타파 대답.
다음과 같이 사용됩니다.
NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>"
textFont:[UIFont systemFontOfSize:10]
textColor:[UIColor grayColor]];
[webView loadHTMLString:htmlString baseURL:nil];
나를 위해 일한 또 다른 쉬운 방법
UIFont *font = [UIFont systemFontOfSize:15];
NSString *htmlString = [NSString stringWithFormat:@"<span style=\"font-family:Helvetica; font-size: %i\">%@</span>",
(int) font.pointSize,
[bodyText stringWithNewLinesAsBRs]];
[myWebView loadHTMLString:htmlString baseURL:nil];
그냥 변경하십시오
font:helvetica
에게
font-family:helvetica
나는 사용한다
body {
font-family: 'Noteworthy-Bold';
}
내 앱의 나머지 부분은 Apple의 "주목할만한 볼드"를 사용하기 때문에 내 약 페이지 (WebView)에 적합합니다.
자신의 글꼴을 사용하려면 @font-face를 사용하고 TTF 또는 EOT 파일을 참조하십시오.
더 많은 글꼴 설정으로 사용하기 쉽고 확장하기 쉬운 솔루션입니다.
myHTMLLabel = [[UIWebView alloc] initWithFrame:CGRectMake(myX,myY,myWidth,myHeight)];
myHTMLLabel.userInteractionEnabled=NO;
myHTMLLabel.scalesPageToFit=NO;
[myHTMLLabel setOpaque:NO];
myHTMLLabel.backgroundColor = myBackColor;
NSString *myHTMLText = [NSString stringWithFormat:@"<html>"
"<head><style type='text/css'>"
".main_text {"
" display: block;"
" font-family:[fontName];"
" text-decoration:none;"
" font-size:[fontSize]px;"
" color:[fontColor];"
" line-height: [fontSize]px;"
" font-weight:normal;"
" text-align:[textAlign];"
"}"
"</style></head>"
"<body> <SPAN class='main_text'>[text]</SPAN></body></html>"];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[text]" withString: myText];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontName]" withString: myFontName];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontSize]" withString: myFontSize];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontColor]" withString: myFontColorHex];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[textAlign]" withString: myFontAlign];
NSLog(@"*** renderHTMLText --> myHTMLText: %@",myHTMLText);
[myHTMLLabel loadHTMLString:myHTMLText baseURL:nil];