4

enter image description here

enter image description here

I have developed a reader that shows on a webview the content of an html file after adding horizontal paging to it using .css. Everything was working fine but on iOS7 I have noticed that the webview is getting clipped at the left edge.

I have tried the following:

readingWebView.frame = CGRectMake(0, 0, 768, 920);   
[readingWebView loadHTMLString:loadString baseURL:nil];
readingWebView.autoresizingMask = UIViewAutoresizingFlexibleHeight |        UIViewAutoresizingFlexibleWidth;
readingWebView.clipsToBounds = false;
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets = NO;

This is the css file I have:

html {
height:820px;
//margin:0px;
font-size:24px;
width:628px;
}

body {
margin:0px;
    padding:0px;
    width:628px;

}

#viewer {
    width:628px;
    height:820px;

}

#book {
width:628px;
height:820px;

margin-left:50px;
margin-right:50px;
//margin-top:100px;

-webkit-column-count:auto;
-webkit-column-width:668px;
-webkit-column-gap:140px;
text-align:justify;

 }

.h {
margin-top:8px;
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
coder
  • 5,200
  • 3
  • 20
  • 45
  • Could you post some initialisation code? – Sebastian Borggrewe Feb 14 '14 at 12:12
  • There is no initializing for readingwebview its only defined in .h to be a uiwebview and added in synthesize in .m – coder Feb 14 '14 at 12:55
  • Are you initialising the webView through Storyboard? + can you please post a screenshot? – Sebastian Borggrewe Feb 14 '14 at 13:31
  • Sebastian thank you for your assistance, no I'm not using a storyboard. in .h I put IBOutlet UIWebview *readingWebView; then I add the proprety and in .m I add synthesize and release. in the nib file I link the webview to the webview. – coder Feb 14 '14 at 14:07
  • Could you post a screenshot of the nib file and a screenshot of the simulator showing the webview? – Sebastian Borggrewe Feb 14 '14 at 16:52
  • I posted the screenshots I'm sorry the nib file is showing twice. thank you for your help – coder Feb 15 '14 at 09:09
  • Show some more code : – Samkit Jain Feb 18 '14 at 14:16
  • would kindly clarify what exactly you wish me to add in specific, for my code is huge – coder Feb 19 '14 at 06:16
  • Can you scroll the webview. Meaning: Is the html content of your webview bigger, than your webview? Also try to color your html body in red or whatever to see whether that is the case. – Sebastian Borggrewe Feb 20 '14 at 11:35
  • Ok one more thing: You are defining the Html part to be: 628px. But you are also defining the book part to be 628px + you are adding a 50px margin at both sides. Try setting the body width to 728px and remove the width in the html element – Sebastian Borggrewe Feb 20 '14 at 11:37
  • Sebastien thank you for your tips, indeed the width was originally 668 and as we make margins from the left and the right side this will add 100, and it will make the current width of the web view. Yes I do have the possibility to scroll horizontally in the webview for its a book, so I have the paging option that works fine. I removed the width in the html and I'll post a screenshot of the nightmode so you may see the borders of the webview – coder Feb 20 '14 at 13:07
  • in this screenshot I have changed the text color and the webview's background color. – coder Feb 20 '14 at 13:16
  • So does this solve the issue? I am sorry I can't really tell whether the letters are being cropped off, since I cannot read them :) – Sebastian Borggrewe Feb 20 '14 at 19:19
  • no problem Sebastien :), yes the words are still being cropped off at the left edge not all the lines but some of them, and I'm running out of time, the weird thing is that this is only happening on ios 7, is there any other suggestion that I can try. – coder Feb 21 '14 at 05:40
  • Have you tried a tool like Reveal or Spark Inspector? You can then have a look at the view hierarchy and figure out, whether this is an issue with your HTML file or with the UIWebView – Sebastian Borggrewe Feb 24 '14 at 12:55
  • thank u Sebastien, but if the problem was with the html wouldn't it have occurred with previous iOS as well. Anyway, I shall give it a try would u kindly lead me into how to use spark inspector or reveal? – coder Feb 24 '14 at 13:01

4 Answers4

2

I think this is a CSS issue. Apparently webkit has a few different text clipping issues that can be solved just by adding   at the end of any text line. Directly after a word has been clipped.

Could you try modifying one of your html files to add   after the letters being clipped and see if it fixes it? If so we can pursue writing javascript that automatically inserts   at the correct places!


edit: If it does work use this javascript code:

var bodyText = "هذا هو بلدي النص الأساسي.";
var newBodyText = bodyText.replace(" ","  ");

On second thought, since you are an RTL string you might need to use this instead, not sure how string.replace function handles RTL

var bodyText = "هذا هو بلدي النص الأساسي.";
var newBodyText = bodyText.replace(" ","  ");

edit edit:

To do this via RegEx (ignoring HTML <*>script tags, in Objective-C I believe you would use this code (I don't know much about RegEx so if it didn't work I would just open a new question with the RegEx tag about how to do this, they can probably get you an answer within minutes!)).

NSString *string = originalHTMLString;

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?i)(<script(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</script\\s*>|<style(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</style\\s*>|<textarea(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</textarea\\s*>|</?[a-z](?:[^>\"']|\"[^\"]*\"]|'[^']*')*>|\\S+)|\\s+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"&nbsp; "];

finalHTMLString = modifiedString;

... Again, on second thought, since it's an RTL language you may actually use this variation of the code which puts the nbsp AFTER (left to right) the space:

NSString *string = originalHTMLString;

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?i)(<script(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</script\\s*>|<style(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</style\\s*>|<textarea(?:[^>\"']|\"[^\"]*\"]|'[^']*')*>)\s+</textarea\\s*>|</?[a-z](?:[^>\"']|\"[^\"]*\"]|'[^']*')*>|\\S+)|\\s+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@" &nbsp;"];

finalHTMLString = modifiedString;
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • I shall give it a try right away. Thank you for your tip – coder Feb 24 '14 at 13:42
  • I afraid I cannot apply this code cause the space can be part of the html code, for instance you may have: بالمئة فقط وفئران حقول البراري as you notice you have a space between samp and class, with this code you'll be inserting &nbsp even between samp and class – coder Feb 24 '14 at 14:07
  • @coder But did the ` ` work? We can adjust the javascript later :) – Albert Renshaw Feb 24 '14 at 14:50
  • @coder Do you have the body text inside of a
    or something? If so we can use the div ID and only find and replace the inner-text. Unless you have html inside your body text as well, in which case we will have to use RegEx.
    – Albert Renshaw Feb 25 '14 at 17:12
  • @coder If you could post an html file that would be great! Also, do you plan on using find and replace to add nbsp after each space via Javascript or via Objective-C ? – Albert Renshaw Feb 25 '14 at 17:16
  • I'm sorry for the late reply, indeed the body contains html code, I prefer using objective c for it is much faster. – coder Feb 26 '14 at 07:51
  • @coder Added objective-c which takes a string of the entire HTML file and replaces it with a string that contains the entire HTML file with the NBSP added. Try both variants, let me know if they work! If not you will need to open a separate question on StackOverflow about how to replace space with NBSP in Regex WHILE ignoring – Albert Renshaw Feb 26 '14 at 08:12
  • 1
    Thank you Albert for your support, I truly appreciate it, I will find a way to deal with Regex no worries. Thank you again and good luck for you too. – coder Feb 26 '14 at 08:18
  • Albert I just noticed your edit, thank you for your help, unfortunately it didn't work I think the regex expression is wrong though I noticed that you took it from the link you posted before, but it gave null as a result I'll open a new question and I'll post a sample html. – coder Feb 26 '14 at 08:57
  • http://stackoverflow.com/questions/22036410/replace-space-in-text-without-affecting-html-tags I have posted my question here. – coder Feb 26 '14 at 09:05
1

We also use UIWebView for some special content like presentation and e-books in our project and I will recommend you use viewport to make content feet the bounds.

Is easy to google details, or you can see docs on apple dev center Configuring the Viewport

sage444
  • 5,661
  • 4
  • 33
  • 60
  • Thank you for your tip, I have inserted the following in my html: but still having the same problem, I also tried but didn't solve the issue – coder Feb 20 '14 at 06:24
1

I don't really know what the problem is, but maybe Apple has a view inside the webViewDelegate that has clipping enabled by default? ... Can you try this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"Adjusting SubView Clippings");//<-- Wait until this logs otherwise this code hasn't been called yet, even if webView looks like it's been displayed!
    for (UIView *view in [webView subViews]) {
        view.clipsToBounds = NO;
    }
}

And also I can't see your whole project file but see if this works:
view.superview.clipsToBounds = NO;

Also, perhaps the way UIWebView interprets CSS margin tag is the culprit? Have you tried loading the page without the left and right margins and then just shrinking your UIWebView's width to create the margins in objective-c instead. Does this fix it? (clipToBounds must remain false, of course haha)

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • thank you, for your tips, I have tried to remove the margins but still same result, as for clipsToBound I have set to false in the nil file for the uiwebview, but nevertheless I will try the code you have posted maybe as you said the webview contains any subview by default. – coder Feb 24 '14 at 06:59
  • @coder Added a different answer on this page, check it out and let me know! Also if you could post a JSFiddle of one of your demo html files that would be GREAT! I do believe this is a CSS bug or at least a bug on how iOS renders CSS now-a-days, either way I don't think objective-c is going to fix your bug. – Albert Renshaw Feb 24 '14 at 13:42
1

This is a known bug in WebKit. It happens in all RTL languages. For our app, it happens when our meta tag viewport is not wide enough to capture the entire RTL element.

If you are presenting only formatted text and possibly images, I recommend moving to UITextView.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • thank you for your advice but this impossible, our html is well worked on and essential for to be displayed in a web view. – coder Feb 24 '14 at 06:56
  • @coder Are you sure you cannot load the HTML into a `NSAttributedString` and display in text view? It might work well. Here is the documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html – Léo Natan Feb 24 '14 at 07:14
  • yes because we leave implicit markers in the webview to track the place of information, plus I have the highlight feature included, and I usually do it manipulating the html. – coder Feb 24 '14 at 08:05
  • @coder How about extending the meta view port to just a bit wider to fix this problem? – Léo Natan Feb 24 '14 at 08:55
  • I have tried this but it didn't work (nothing has changed), do u suggest any other input for the width – coder Feb 24 '14 at 09:24