I thought this would be simple but it's proving not to be.
I have some text that I need to display in my app. What is the best way to present it? Because I want the text but also want a header - the header will be larger. I know UITextView only allows 1 type of font/size.
Example of what I want
Example Header - Bold size 22
Text - size 16
How do I do this?
If your text is not editable, I think you can think about UILabel instead of UITextView, and you can use more complicated view hierarchy, not just an UITextView or UILabel.
If you insist using UITextView, I think you can refer to NSAttributedString.
Related
Could somebody explain how to make a UITextField within a UITableView cell expand vertically. Right now when I add text to the textfield it expands infinitely but in the horizontal direction. Is there a way to have the textfield use a fixed width and expand vertically? If there is could somebody provide sample code for this kind of functionality?
The answer would be to use a UITextView, not a UITextField.
Check this out - I created a tool that makes a uiview which holds a clickable/expandable uitextview. You can use this, set the size of the containing cell accordingly, and overwrite the functions that change height programmatically.
It's not exactly what you wanted, but it might help!
https://github.com/AlphastudioNJ/nicktitle-ui-tools/tree/master/GrowingUITextViewer
I want to have a UISegmentedController but the user can choose the segment and change the text inside. Is there any way to do that?
Thanks.
EDIT: This is what I would like to do. I have users which choose an option using UISegmentedControl. Under settings, I would like to give them the opportunity to modify what the texts in the UISegmentedControl is. I can use a UITextField and a button which triggers the change but I didn't like that solution. I considered putting a UISegmentedControl image with UITextField inside each segment but that does not seem like an elegant solution. Besides, I have no idea what the font is being used in UISegmentedControl. If I am not mistaken, by default, you can only fit a maximum of 12/13 chars or else it can break. iOS 5 SDK has some more methods to customize so it may be possible.
I coded a solution which involves a preview of what the UISegmentedControl will look like, 2 textfields to replace the texts inside each segment and a button which saves the value. I scraped it because I did not like the implementation and I just didn't like the execution too. I wanted the users to feel like they are using the UISegmentedControl and able to edit the texts inside it, up to a max of 12/13 chars.
Thanks.
You can always dynamically change the values of the segmented controller in code. Would it be acceptable to have the user click the button, at which point you could pop up a form for the user to fill in with the new value, and then call this method on the segmented control:
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment
I want to make a compiler-esque UITextView subclass to allow for different sections of the text to be different colors, because I know that normally a UITextView only allows for 1 color for the whole thing. Does anyone know how to do that? And I dont want to use UIWebView because I want it to be editable.
If you want to have fully customized text, then the simplest (best?) thing to so is simply to use a UIWebView instead.
Take a look at CATextLayer or, if you need more than simple rendering, CoreText.
Say I wanted to have the first six characters in black and the next six characters in blue, then the last 20 characters in black of a UILabel. Is this possible?
Thanks!
In Big Cocoa you would use an NSAttributedString in a NSTextField or NSTextView. In Cocoa Touch you use a UIWebView to display rich text.
Zynga has done a lot of the heavy lifting for you on this one, check out Font Label on GitHub.
As Darren said, NSAttributedString is not supported on the iPhone but you could use a UIWebView.
Another possible solution might be to draw the text using NSString's -drawAtPoint:withFont: method (or similar). You could subclass UILabel and do the drawing in -drawRect:.
First, set the color to black and draw the first six characters. Note the width that -drawAtPoint:withFont: returns, and use it to calculate the starting point for the next six characters. Change the color, draw those characters, and then repeat the same for the remainder of the string.
Note that -drawAtPoint:withFont: doesn't do line breaks and stuff like that, so it could quickly get more complicated than what I've described.
Have a look at the NSString UIKit additions.
Give TTTAttributedString a try. It's a drop-in replacement for UILabel that renders NSAttributedStrings like a champ.
You want an NSAttributedString.
How do I get justified text with UTTextField. It does have an textAlignment property. But the UITextAlignment constant only has left, right, and center justification.
What I am seeking is the Justified text common in word processing app with text flush with both left and right edges. This is a read only text field.
I have seen it in few iPhone apps. So it seems I am missing something.
It's a bit of an overkill maybe, but one way seems to be to use a WebView for it and style the text with CSS.
UILabel and UITextField do not support full-justified text. If you want it for a UITextField, you would have to create a subclass of UITextField and override drawTextInRect:, splitting the text into words and using sizeWithFont: to figure out how to space them along each line.
I cannot imagine what would be worth the trouble.