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.
Related
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.
I have two UILabels, both will have the dynamic text but interrelated.
for example:
label1.text=#"Abc";
label2.text=#"Meaning of Abc is ......";
Now label2 will always have label1.text plus some extra text.
I want to make this part in label2 to be in Bold.
So far I found that it could be done by drawing the text in drawRect but that seems to be very complicated.
Is there any other way out is possible??
Thanks
Ben
iOS 6 will supported attributed label for this sort of thing. Until then or if iOS 5=< compatibility is important to you, you can make the label a UIWebView and use HTML markup. Or do it the hard way with drawRect (which will perform better).
Edit: There are third party libraries that add attributed labels, Nimbus is one of them that works well.
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
I would like to have a text in a text... I mean like in the newspaper: when there is a picture, the text fits and follow the border of the picture.
For example: http://en.wikipedia.org/wiki/Iphone
look at the pictures... the text follows them!
Is it possible to do that on the iPhone?
Thanks in advance
You can achieve this fairly easily with a UIWebView and traditional web techniques.
If you want to have it happen in Cocoa directly, you're going to have to dive into the more advanced CoreText APIs, which will probably be a lot of work.
It's called text wrapping or, in CSS/HTML speak, "floating" an image. The best way to do it is probably to have a UIWebView and programmatically dump HTML and CSS into it.
The HTML looks like this:
<img src="IMAGE_URL" style="float:right; margin:6px;" />
<p>My block of arbitrary test. Blah blah blah. Etc.</p>
Here's an example of how to use UIWebView with a string of HTML:
http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview
Maybe I'm missing something, but why not use subviews? Have a UIView containing a UIImage and UILabel in case of a static text or a non-editable UITextView in case of dynamic amount of text and set their properties to be aligned to the parent view's boundaries.
If you still want to take the low-level path, google for CGContextShowTextAtPoint.
You may also check out this: http://code.google.com/p/core-plot/source/browse/framework/Source/CPTextLayer.h
I have an app which shows text in a UIPickerView based on a search. I want to highlight specific letters in the string that are there as a result of a wildcard character.
For example, if I searched for "CA?", one of the rows will show "CAT" and I want only the letter "T" to be in the color blue.
Any ideas? The user gets immediate feedback as he types so performance is important.
To create a string that has different font properties for different characters, you would generally use NSAttributedString. However, UIPickerView doesn't seem to directly support using NSAttributedStrings as the labels for your picker components, nor does UILabel seem to support them. You might have to create a custom UIView subclass and return it from pickerView:viewForRow:forComponent:reusingView: in your UIPickerViewDelegate.
Thanks to David for the tip to get me started.
I ended up using the Three20 library and returning a TTStyledTextLabel from pickerView:viewForRow:forComponent:reusingView: with the text property set to [TTStyledText textFromXHTML:myXHTML] along with a TTDefaultStyleSheet to define the colored spans. Works great and seems to be very fast in the UIPickerView component.