How do I set a highlight colour for text in a UIWebView? - iphone

Our app uses a lot of custom UITableViewCells. Most have a few extra UILabels, and in those we can set the highlightTextColor property of the label so that when a user selects the cell, the text changes to white.
However, some of them require extra formatting, so we use UIWebViews with HTML contents; the text colour is set by the CSS embedded in the HTML. When the cell is selected, the text stays whatever colour is set in the CSS.
Is it possible to set a highlight colour for the text in the UIWebView? If so, how - is there a special CSS selector? I tried -webkit-tap-highlight-color but it had no effect.

As it appears, you can't. The -webkit-tap-highlight-color selector is used when you tap (or even tap and hold) over a link. However, the selection feature is native for iOS, and there isn't any known way to override its settings, be the highlight color or anything else.
You could try the ::selection selector, but I have had no success at all. In regular browsers it works like this:
p.normal::selection {
background:#cc0000;
color:#fff;
}
p.moz::-moz-selection {
background:#cc0000;
color:#fff;
}
p.webkit::-webkit-selection {
background:#cc0000;
color:#fff;
}
However, in mobile webkit it has no effect.

Related

how can I highlight only background of the text on touch on a UILabel

In my app, I have modified the UILabel and made it underline the text if the phone number or email appears.
but I want to highlight the text when the phone number or email is touched as shown below (I want something like what has been shown below by the dark gray highlight in background of the text "http://www.foodreporter.net".
I want to do this without use of CoreText as my app targets from iOS 3.1.2 onwards. So I don't want to use the CoreText and Attributed strings.
I want to subclass the UILabel and do it somehow. How can it be done?
I don't have the option to use OHAttributedLabel and TTTAttributedLabel.
I suggest for a workaround. What you can do is, use custom UIButton instead of label. Set the default and highlighted image of the button according to your need.(Simple text for default, and highlighted text for highlighted).
Happy Programming

Xcode UITextView Change textcolor of specified word

I would like to change the font color of a certain word inside a UITextView.
if([text1.text isEqualToString:#"value"]) {
}
That's what I have right now, and I want the word value to change font color, any ideas? Sorry if it's simple but I'm still learning ;)
It's not possible, sorry.
from UITextView Class Reference
This class does not support multiple
styles for text. The font, color, and
text alignment attributes you specify
always apply to the entire contents of
the text view. To display more complex
styling in your application, you need
to use a UIWebView object and render
your content using HTML.

How to change the colors of HTML form elements displayed in UIWebView?

I have an application that opens HTML pages in a UIWebView. The pages have a specific color theme, which is disrupted by form elements on the page (I have few "select" fields), which have their own color theme (white font, light gray background). CSS can't seem to force it to display in other colors.
Does anyone know how to fix that?
Thanks!
Try using -webkit-appearance: none; to disable the browser's default styling, then apply your own styles. More info and examples at 37signals.
Try using !important in your external CSS styles. It could be that the style sheets for UIWebView are inline and are overriding your external styles.
For example, select { color:#000!important; } should override any inline style text color (unless it is set to !important as well) on <select> elements.

iphone- Highlighting div on tap that isn't a link

I'd like to highlight an entire div when the user touches or selects the text contained in the div. I'm able to highlight the text only when it is contained in a span with a javascript function, but when I try to apply the function to the entire div it does not work. In my research I've seen many people remove a highlight with -webkit-tap-highlight-color:, is there a way to use this or another css style to add a highlight to an item that is not a link?
The script I am using:
function changeColor(e,color){
element = e;
oldColor = element.currentStyle.background;
element.style.background = color;
}
If you attach a click handler to the div Safari would highlight the div on click.
An Example is here http://jsbin.com/awejo3/4
Got the information from this question: Iphone darkens div on click
I was facing the same issue on a mobile app and I have resolved issues with the below code.
*:not(a) {
-webkit-tap-highlight-color: rgba(0,0,0,0);
}

Why would a link in a UIWebView leave a gray box behind?

I have a UIWebview that I am loading with custom HTML using loadHtmlString. My HTML has a link that I intercept when tapped so that I can take control and reload the UIWebview with a different HTML string. The code works, but the area where the link text was located is replaced with a gray rectangle that is visible when the new string is loaded.
What could be going on here? How can I get rid of this behavior?
//Scott
Here it is...need to add this style statement to the link reference:
<a href=http://yourlink.com/ style = "-webkit-tap-highlight-color:rgba(0,0,0,0);">
Setting the alpha value (the last parameter to rgba) to 0 disables the tap highlight color.
Although this works, it feels like a hack. I really think the tap highlight should clear itself when my second loadHtmlString reloads new HTML code.
//Scott
You could add onclick="this.blur(); location.href=this.href;" to the link to remove the focus from the link.
I'm sure there's a better less hackish way, but this should work