ビューを表示せずに、特定のコンテンツで UIWebView のサイズを計算しようとしました。サイズだけわかればいいんです。
私の問題: コードを実行すると、UIWebView のデリゲートが呼び出されません。なんで?
MessageSizeCalculator.h
#import < Foundation/Foundation.h>
@class Message;
@interface MessageSizeCaluclator : NSObject <UIWebViewDelegate>
- (id)initWithMessage:(Message*)message;
- (void)saveSize;
@end
MessageSizeCalculator.m
#import "Message.h"
#import "MessageSizeCaluclator.h"
@interface MessageSizeCaluclator () <UIWebViewDelegate>
@property (strong, nonatomic) Message* message;
@property (strong, nonatomic) UIWebView* webView;
@end
@implementation MessageSizeCaluclator
@synthesize message = _message;
@synthesize webView = _webView;
- (id)initWithMessage:(Message*)message
{
self = [super init];
if (self) {
_message = message;
// WebView
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_webView.delegate = self;
}
return self;
}
- (void)saveSize
{
NSLog(@"%s message = %@", __PRETTY_FUNCTION__, _message.text);
[_webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-family:Helvetica;font-size:13px;'>This is a test</div>", _message.text]
baseURL:nil];
}
#pragma mark - Web view delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
@end
実装
MessageSizeCaluclator* messageSizeCalculator = [[MessageSizeCaluclator alloc] initWithMessage:message];
[messageSizeCalculator saveSize];