I have the following UIWebview.
webNieuws = [[UIWebView alloc]initWithFrame:CGRectMake(10, height,300, 400)];
[webNieuws setScalesPageToFit:NO];
[[webNieuws scrollView] setBounces: NO];
webNieuws.delegate = self;
NSString *myDescriptionHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; size=\"12\" COLOR:#111111; background-color:transparent;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"MyriadPro-Regular",_hotnews.hot_content];
NSLog(#"Hot news content is %#",_hotnews.hot_content);
[webNieuws setBackgroundColor:[UIColor yellowColor]];
[webNieuws setOpaque:NO];
[webNieuws loadHTMLString:myDescriptionHTML baseURL:nil];
[scrollView addSubview:webNieuws];
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame2 = aWebView.frame;
frame2.size.height = 1;
frame2.origin.y = height;
aWebView.frame = frame2;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame2.size = fittingSize;
CGFloat webHeight = [[aWebView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
frame2.size.height =webHeight;
aWebView.frame = frame2;
NSLog(#"Height is %f",webHeight);
float newHeight2 = 30 + webHeight;
NSLog(#"new height header is %f",newHeight2);
[scrollView setFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width,newHeight2 + 320)];
scrollView.bounces = NO ;
}
The same webview gives a different result, like you can see below. I know I can set scalespageTofit. But then I loose my fontsize.
Can someone help me with this ?
Please try this in webViewDidFinishLoad method
NSString *javascriptCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.0;"];
[aWebView stringByEvaluatingJavaScriptFromString:javascriptCommand];
If this doesnot work then check your webView's autoresing property
Related
I have taken a web view and load html like this
NSMutableString *myDescriptionHTML = [NSMutableString 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:40], messageModel.content];
i want to set web view height to web view content size height after web view loaded html content
Tried ways
1)
CGRect frame = self.webViewMain.frame;
frame.size.height = 1;
self.webViewMain.frame = frame;
CGSize fittingSize = [self.webViewMain sizeThatFits:CGSizeZero];
frame.size = fittingSize;
self.webViewMain.frame = frame;
Height = fittingSize.height;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
[tableViewMain reloadData];
its too long height
2)
Height = [[self.webViewMain stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
3)
Heiht = self.webViewMain.scrollView.contentSize.Height
But no one is actual height
help me thanks....
UIWebview is a subclass of UIScrollview so you can set Webview.content size using CGSize. That will set your web view content height and width as required.
I am using NSAttributedstring with the attribute kCTUnderlineStyleSingle.
Character is underlining properly. But some character like g, q, p are not visible properly.
Even user cannot differentiate between g and q.
Is there any solution for that?
You can fix it using this method:
- (void)addUnderlineWithRange:(NSRange)range
{
int lineThickness = [EnvUtils isIpadEnv] ? 2 : 1;
UIColor *lineColor = self.textColor;
NSString *string = self.text;
CGRect labelFrame = self.frame;
CGSize expectedLabelSize = [NSString getMultilineTextSize:[string substringWithRange:range] forMaxSize:labelFrame.size font:self.font];
CGSize expectedLabelFullSize = [NSString getMultilineTextSize:string forMaxSize:labelFrame.size font:self.font];
CGSize xOffset = [NSString getMultilineTextSize:[string substringWithRange:NSMakeRange(0, range.location)] forMaxSize:labelFrame.size font:self.font];
CGFloat originX = (labelFrame.size.width - expectedLabelFullSize.width) / 2 + xOffset.width;
CGFloat originY = expectedLabelSize.height + (labelFrame.size.height - expectedLabelFullSize.height) / 2;
CGRect lineFrame = CGRectMake(originX, originY, expectedLabelSize.width, lineThickness);
UIView *lineView = [[[UIView alloc] initWithFrame:lineFrame] autorelease];
lineView.backgroundColor = lineColor;
[self.superview addSubview:lineView];
}
I have a list of 15 questions each of them again have sub questions, I'm showing these questions on webView which is present in tableViewCell. Below to this webView there are three labels and three buttons. Now problem is that I'm not getting the heights of tableViewCell based on webView's content. Please suggest me a good solution and thanks in advance.
Here is my code in webviewdidfinishload
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
int fontSize =160;
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[aWebView stringByEvaluatingJavaScriptFromString:jsString];
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}
I have a UIImage, a UILabel and a UIWebView inside a UIScrollView. Basically, I'm calculating the height of both the UIImage and the UILabel and adding it to the UIWebView height to set the UIScrollView's contentSize.
Here's how I calculate the UIWebView height:
- (void) webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
CGSize fittingSize = [webView sizeThatFits:webView.scrollView.contentSize];
frame.size = fittingSize;
webView.frame = frame;
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, frame.size.height + y + 10);
[webView setFrame:CGRectMake(x-8, y, webView.frame.size.width, frame.size.height)];
[webView setContentMode:UIViewContentModeScaleAspectFit];
}
The problem is for some cases the UIWebView is cut on the bottom... Any ideia why?
This might be relevant: the UIWebView content is HTML code I have no control on.
try out the following code:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = WebView.frame;
frame.size.height = 1;
WebView.frame = frame;
CGSize fittingSize = [WebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
WebView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}
I am building a simple app which displays a users Contacts with photos like this -
The contacts are each a positioned subview, with another subview inside containing a UIImageView to generate the images w/rounded corners and a UILabel.
Scrolling becomes rather choppy on both iPhone 4/4s and iPhone 5, however, even with only 6-7 contacts. Also, the choppyness seems to be constant. It doesn't matter if they have 8 contacts or 500 contacts, it doesn't get worse or better either way.
Does anybody know why this might be? The algorithm that generated this grid can be seen below. Are there any specific properties on the UIScrollView I haven't set, etc?
- (void) generateContactGrid
{
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(self.addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(self.addressBook);
int peopleInARow = 0;
int maxPeopleInARow = 3;
int positionFromTop = 20;
int IMAGE_SIZE = 70;
float animationOffset = .5;
float animationOffsetChange = .3;
float animationDuration = .5;
int scaleOffset = 40;
int screenWidth = 320;
int startingPositionFromLeft = 26;
int positionFromLeft = startingPositionFromLeft;
int topOffset = 40;
int leftOffset = 26;
int numberOfRows = 0;
UIView *contactContainer;
UIImage* image;
CALayer *l;
NSString *name;
NSString *lastName;
NSString *firstName;
UIImageView *newimageview;
UILabel *label;
UIView *contactImageContainer;
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
name = [NSString stringWithFormat:#"%# %#", firstName, lastName];
if(ABPersonHasImageData(person)){
NSLog(#"Current Contact Being Generated: %#", name);
image = [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)];
NSLog(#"Contact's Position From Left: %i", positionFromLeft);
newimageview = [[UIImageView alloc] initWithFrame:CGRectMake(-scaleOffset/2, -scaleOffset/2, IMAGE_SIZE+scaleOffset, IMAGE_SIZE+scaleOffset)];
newimageview.contentMode = UIViewContentModeScaleAspectFit;
[newimageview setImage: image];
contactContainer = [[UIView alloc] initWithFrame:CGRectMake(positionFromLeft, positionFromTop + 20, IMAGE_SIZE, 200)];
contactImageContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_SIZE, IMAGE_SIZE)];
contactImageContainer.clipsToBounds = YES;
l = [contactImageContainer layer];
[l setMasksToBounds:YES];
[l setCornerRadius:IMAGE_SIZE/2];
[l setBorderWidth:0.0];
[l setBorderColor:[[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:.6] CGColor]];
[contactImageContainer addSubview:newimageview];
[contactContainer addSubview:contactImageContainer];
label = [[UILabel alloc] initWithFrame: CGRectMake(0, IMAGE_SIZE + 10, IMAGE_SIZE, 20)];
label.text = firstName;
label.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
label.textColor = [UIColor whiteColor];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:14]];
[contactContainer addSubview:label];
contactContainer.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationDelay:animationOffset];
animationOffset+= animationOffsetChange;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
contactContainer.alpha = 1;
CGRect temp = contactContainer.frame;
temp.origin.y = positionFromTop;
contactContainer.frame = temp;
[UIView commitAnimations];
if(peopleInARow >= 2){
positionFromTop += IMAGE_SIZE + topOffset;
peopleInARow = 0;
positionFromLeft = startingPositionFromLeft;
numberOfRows++;
} else {
peopleInARow += 1;
positionFromLeft += IMAGE_SIZE + leftOffset;
}
[self.scrollView addSubview:contactContainer];
[self.scrollView bringSubviewToFront:contactContainer];
}
}
NSLog(#"%i", numberOfRows);
self.scrollView.contentSize = CGSizeMake(screenWidth, 150 * numberOfRows);
}
Use Instruments to measure what exactly makes your app slow. Everything else is just guessing.
But I will guess anyways. This most likely is the masking you apply to your UIImageViews. Try disabling that and see if it is still slow (and by see I mean measure, of course). If that is indeed the cause there are two things you can try. First you can set the shouldRasterize property of the masked layer and see if that actually is enough. This could make things worse though if your layers get changed a lot. The other option would be to render those masked pictures with Core Graphics instead of using layers.