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.
Related
Xcode 8.3.2
As as newbie, I'm trying to get acquainted with segues, constraints, text fields and Image Views...mainly the design elements. I'm trying to put together a cheesy recipe book of favorite recipes. I wanted to know what's the best approach to display rich text with bold text and bullet points? Labels seem to be my only option but I was wondering if there were other options? Any advice would be appreciated.
ViewController in Storyboard
As much for my approach, during my career I learned that textViews (aka C# RichTextBoxes) can be pretty tricky..
You can go with classical UILabel approach with lines in Interface Builder set to 0 and atrributedText, which contains bullet points:
This is a nice tutorial, but a bit old and would probably do it another way, but I guess for the purposes is fine...
https://wingoodharry.wordpress.com/2016/04/10/bullet-point-list-ios-swift/
I would like to state here, that TextViews are the last option everywhere with text...
Wish happy coding! :)
UILabels are best way to display text, you can use attributedText in UILabels which will enhance your UI. Multiline text is a add on.
Else try using AsyncDisplayKit.
Here is what I am trying to achieve. Instagram notification cell style:
I have a UIButton, and 2 UILabels, each with separate styles. They are currently all individual, but I want them to look like one string. So no matter how many line breaks, label 1 follows the end of the UIButton title, and wraps around it if multiple lines. And label 2 follows the end of label 1 no matter how many line breaks.
I have looked into attributed text strings, only I got stuck on keeping the first part (currently UIButton) actionable.
And then I looked into stack views. But for the life of me, I can't see how I could have the text wrap around UIButton title text.
Also worth noting all text is dynamic.
Can someone please help me understand the best approach for this?
I'm using Swift 2.2
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 a table view. in that one row is having different lines
for that i wrote code like this:
cell.textLabel.text=#"Hi this is PRASAD";
here i want only "PRASAD" with different font & must be in BOLD.
what logic i have to implement in textlabel?
You can't. A UILabel cannot have multiple font characteristics. The only option is to make a custom view for the cell that is either a webview or multiple labels.
Use NSAttributedString for holding text and CoreText for rendering.
It is fast & it works but surely not as easy as UILable :)
Look at the core text sample code by Apple
There is also a gitHub project "NSAttributedString + HTML" look it up.
There are a lot of CoreText projects as well on gitHub.
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.