How do I apply CSS to style the text in a UILabel? - iphone

I would like to be able to style the text in a UILabel using CSS. I know that you can use a UIWebView to load HTML to be formatted for display using
[webview loadHTMLString:myText baseURL:nil];
but I would like to simply style just a UILabel without relying on UIWebView. How can I do this?

Look at the open source project called FontLabel at http://wiki.github.com/zynga/FontLabel.
Although it won't let you apply a CSS file to a UILabel, it does provide a subclass of UILabel that can use custom fonts and styling.
Of course you can also use the standard properties of UILabel like color, textAlignment, etc.

Since css file structure is very easy you could create a simple css parser that searches a certain id or class. Then get the font size, name, style and other attributes and instead of using UILabel you maybe want use CATextLayer because text is much more customizable (colors, font, size, style, and even more than one fonts in a single line)
But I wonder why do you need a UILabel and not UIWebView?
Hope it helps

Related

How to keep 2 different fonts within the same UITextField or UITextView?

I want to set 2 different fonts within the same UITextField and UITextView . How to do it?
Its a bit of work - you'll need to use Core Text and NSAttributedString to do this.
There are plenty of tutorials and examples, although I'd suggest using someone else's already-made UILabel subclass such as:
OHAttributedLabel
or
TTAttributedLabel
As these usually have some convenience methods to make handling a lot easier.
I would do it with 2 custom textfields overlaying, both backgroundcolor:clearColor, maybe stuffed on an image that represents the background.
I don't think it is possible to handle 2 different fonts within the same UITextField or UITextView. If you want to have different font style you can either set different font style within a UIWebView or use the coreText API.
Here are some links that might help:
iPhone Development - Setting UIWebView font
the official doc on core text: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
I know you already picked a valid answer but... don't do it that way... it's not worth it. Use a webview instead and draw everything with html.
In interface builder change Text View's Text field to Attributed. In that small editor that appears you can change the font/format/color of the selected text, like in any advanced text editor.

Difference between UIWebView Fontsize and UItextView Fontsize

Can any one tell me what is the major difference between UIWebView's font size property and UITextView's FontSize property??
Both are different, As UIWebView's Fontsize is very bigger then UITextView's font size, through both are 15..
Please tell me, What is the difference?
Thanks in advance..
Following is working properly.
<html><body style="margin-top:0px; margin-left:0px"><marquee behavior="alternate" style="font-family:verdana;font-size:10px; color:red;">Test</marquee></font></body></html>
although both are rendering HTML but main difference is,
UIWebView giving u the HTML view its font size denpend ViewPort,
where as the UITextView can control its font size.
According to the official Objective C website:
"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 I understood it: on a UIVewView you can use as text HTML tags, in UITextView it will be displayed as a plain text. The UIWebView's font's size depends from the ViewPort, and you have no direct access to it, as well as the UITextView has properties for the font size setup.

iPhone: How to higthlight word of UILabel

I would like to highlight a word which in an UILabel which is placed on an UITabelViewCell.
What I would like to do is set a background color for just that word.
Unfortunately you cannot use a UILabel for this. However, you do have options:
Use a UIWebView instead and set CSS for doing your highlight
<html>
<head>
...
</head>
<body>How is it <span class="highlight">going</span></body>
</html>
Use a custom label library: OHAttributedLabel or TTTAttributedLabel
Add a custom UIView behind your label and postion it over the text
Look into Core Text and create your own custom label
I'd strongly recommend looking at the second option.
Well UILabel does not support this, you will need to use CoreText.
You can use TTTAttributedLabel which wil make easier to use CoreText.
And grab NSAttributedString+Attributes for east manipulation of NSAttributedString.
I don't think this is possible. You need to make several UILabels for separating highlighted text from unhighlighted and put them one by one and, just setting background color of highlighted ones.
... or, don't use UILabel at all, if your text is dynamyc and it will be hard to separate words.

In iOS, how do you embed images inline with text (in a UITextView)?

How can you create a UITextView that can have images inline? I looked up NSAttributedString, but it seems iOS does not support image attachments. Any ideas for an editable text view that can display images (I suppose RTF would do this)?
Thanks in advance for your help!
The only method I am aware of is to use a UIWebView. There is no way to add images to a UITextView, unfortunately, just plain text.
You could use the NSTextAttachment class. It lets you insert images inline of text.
I found an example of this in a cool project here: https://github.com/dzog/ImgGlyph. ImgGlyph provides UITextView and UILabel subclass implementations that help you replace specific strings with images and ensures they're sized correctly with respect to the text surrounding it.

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.