How do I display a NSAttributedString on an iPhone? [duplicate] - iphone

This question already has answers here:
iphone/ipad: How exactly use NSAttributedString?
(9 answers)
Closed 9 years ago.
I believe I understand how to set NSAttributedString values but how on earth do you actually display them in the interface?
examples: UILabel, UITextView, etc.
Specific instructions would be greatly appreciated/
Thanks!

you could also create a CATextLayer, which works fine with attributed strings...
Just create the layer, and set it's string property...
Have fun... :D

If you don't want to bother with CATextLayer, you should check out TTTAttributedLabel. It's just like UILabel, except that supports NSAttributedStrings (using the same setText: method as UILabel, so you can use it as a drop-in replacement)

In iOS 6 and later you can use attributed strings to display formatted
text in text views, text fields, and some other controls. Both AppKit
and UIKit also define extensions to the basic attributed string
interface that allows you to draw their contents in the current
graphic context.
NSAttributedString Reference

The controls you mentioned only work with strings, not attributed strings. The only way to display them is to do a lot of work with Core Text. File a complaint with bugreporter.apple.com to request they flesh this part of UIKit out.

Related

Swift: Text of Mixed Styles

How would I best
format text
of programmatically loaded text
like this, using
differing styles in a Swift app,
preferably within a single string?
Take a look over NSAttributedString and it's mutable version NSMutableAttributedString class.
Also see this tutorial

iOS - stringWithFormat change part of the text color

I'm pulling in tweets for my application and I want to be able to format my tweets in a certain way and color. For example, here would be a simple tweet...
"#somename This is a sample tweet from twitter. - 7 hours ago"
Is it possible to take the last part of the string, in this case the "7 hours ago" and change the font / font color?
I'm using stringWithFormat: in order to format in this way:
tweet = [NSString stringWithFormat:#"%#%#", title, time];
But I am unsure if I am able to change the time font and color using this code.
Anyone done this before?
Edit
I've attempted this using AliSoftware's OHAttributedLabel, but I am using ARC and this doesn't seam to be working for me.
Is there a standard "iOS" way of accomplishing this? Maybe using NSAttributedString possibly?
You could use Core Text. Maybe the following question would help you Is it possible to set different font in One UIlabel?
You have to import CoreText.framework in your App
NSStrings don't know about fonts. Have a look at NSAttributedString and Core Text. References:
Text, Web, and Editing Programming Guide for iOS
NSAttributedString Class Reference
UILabel with two different color text
iPhone - UILabel containing text with multiple fonts at the same time
The font/font color are not inherent attributes of the data type NSString. You could use two separate UILabels, or create a small UIWebView to format the text.

UILabel text Issue in iphone

I have one label which have a Dynamic string data e.g: "I am Mohit" .I want to make only "am" in bold.Is it Possible in iphone? if yes please give me suggestion.Thanks in advance:)
No you can't have text with different styles in standard UILabel. What you'll probably need to use is NSAttributedString and some custom solution to display them. Check this question for some possible options.
Not with UILabel. Suggest 2 labels, or subclass. Many related answers, including this.
No you can't have text with different styles in standard UILabel.
You need to use NSAttributedString and some custom solution to display them. Check this
done something like this using three20's TTStyledTextLabel :) it works perfectly
you can use textView instead of label to fulfill your requirement.
ex:-
[textview setContentToHTMLString:#""];

String with different font

I am having a string message i need first word of that as bold font and rest normal.
Making different label can be create problems,(need to find the size of first the make other and then make other,text is in 4 line).
How can i use NSAttributedString here. I could found How do you use NSAttributedString?
but using this shows undeclared NSForegrou... (present in app kit framework).But could not found app kit framework on sdk 4.2
Need help for making such kind of string(bold + normal text).
Go through the below blog tutorial with code, they have different font with NSAttributedString.
http://www.cocoanetics.com/2011/01/rich-text-editing-on-ios/
https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
https://github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniUI/iPad/Examples/TextEditor
May I point you to the CoreText Framework and Befriending Core Text on cocoanetics.com. This will show you how to format text.

Make portion of NSString italicized or bold, not whole string

How would I go about italicizing a single word in an NSString which will be displayed in a UILabel? Specifically, I don't want all text to be italicized, just one word.
Thanks!
(Edited) I meant UILabel, not UITextField.
I don't think that what you are asking to do is possible (I'd be happy to be proven wrong). However, this library (https://github.com/facebook/three20/) is a popular way to achieve the same result in a UILabel (not text field) . The library works fairly well, but does have a lot of limitations, especially on edge conditions, and of course, it comes with associated overhead.
I'd encourage you to think about other ways of achieving the same user outcome. Can Placeholder text help? How about hints next to your text field?
Good luck.
A native UILabel does not support NSAttributedString which is what is normally used to display strings with formatting. You could try an output the text your self using Core Text but I would suggest checking out FontLabel or the three-20 project mentioned by #JJ Rohrer
Use NSAttributedString... Find controllers to draw NSAttributedString,since UILabel wont support NSAttributedString
Controller for NSAttributedString