Formatted Text in UIScrollview - iphone

What i Need to do for our project is to Display several UIScrollViews in a View and weithin the scrollView a Headline and a Short Text.
I Know how to add a Label to the UIscrollview, but how Gould i handle the Case that the Headline-Label is too Long and wraps to a Second Line.

You can determine the space needed for the headline text by calling -[NSString sizeWithFont:] or one of its variants. Then size and position the labels dynamically in your code (and set their numberOfLines property to 0).
Alternatives:
Use a UIWebView to display formatted text (HTML).
Use Core Graphics to draw the text directly into the view (e.g. [NSString drawAtPoint:withFont:]).
Use Core Text to format and draw the text (quite complicated).

Related

Swift: How to add and animate image view inline in label?

In reference of Swift: make ellipsis continually retype in label? Tough label "animation"?, I am trying to animate little image views to jump in and scale at the foot of some text in a label to create the effect referenced in the above question.
To do this I need to add these image views inline in a label and then animate them using UIView.animateWithDuration and some scaling, etc.
I do not know how to add these images inline in the label so they appear in the same place on the label regardless of device - how can I add image views inline a label in Swift?
You don't "add image views"; you add images as part of the label's text. Use an NSAttributedString. You will need an NSMutableAttributedString so that you can modify it. Make an NSTextAttachment and set its image to your image (needs to be the desired size beforehand, easy to do). Call NSAttributedString(attachment:) to make a second attributed string consisting of the inline image, and insert that new attributed string at the desired index into your original attributed string. Set the label's attributedText to that.

Multiple UILabel(s) repositioning according to wordwrap

I have this interface with multiple UILabels.
On view loading i populate white labelled values with some data from a db.
The problem is, some of that fields are potentially too long for the interface, so i'd like to compute the total height of one label once the text is word wrapped and reposition the 2 labels below (shifting the Y coordinate) accordingly to the previous label's height.
All of this should go inside a UIScrollView to let the user scroll those labels vertically.
Any chance i can do this easily with some control i still don't know, or do i have to do it manually?
Thanks
You'll need to use the NSString UIKit Additions to compute the height you need to set on your UILabel, and then adjust the other controls appropriately.
Specifically, I think you want to use sizeWithFont:forWidth:lineBreakMode: to get the rect for your UILabel.
Alternatively, you could use a UIWebView and display the information as HTML. I don't know if it's necessarily less work, but you'll get a layout that automatically adjusts to the size of its contents.

Spacing between characters on the iPhone

I have a label and I wish to increase the spacing between characters.
I tried adding a space between each character, but this was too much
Perhaps there is a font with large spacing between the letters?
If all else fails, I am considering putting each character (only a size character code), into its own textbox.
Any ideas on how to achieve this?
There is a way to insert a half space, but I don't recall the exact command (option-spacebar?). Wikipedia has a complete list of spaces you can use.
Another approach would be a UIWebView with the letter-spacing CSS attribute set.
You're better off creating a custom view and using your drawRect routine to draw the text manually. You can use CFAttributedString to hold your text along with kerning information.
Update: sounds like you can't actually use CFAttributedString to draw text on the iPhone. You can still use your drawRect to draw the customized text, but it will take some more work to actually get your custom kerning to work.

Multiple UILabels to display decorated XML markup

I have a block of content (stored in XML) that I want to put in a UIScrollView. Certain parts of this text will be formatted with different fonts, sizes, and colors. Altogether, it mostly reads as a paragraph with word wrapping.
I've built my NSXMLParser code, and I have separated all the data. I'm ready to apply my decorations and add these elements as UILabels.
However, I'm looking for a solution to ease the inherent difficulties of string height/width calculations and all of that arithmetic to make these UILabels line up with word wrapping nicely. [keeping track of your last X and Y coordinates, knowing when to insert manual line breaks, how to best vertically display a line that has 2 different sized fonts]
The XML markup can easily be converted to HTML, and thus UIWebView, but I hear that is slower to load.
Is the UIWebView going to be the best class for this? I wish there were one that did all of this with UILabels so that I can use these elements for touch events. (I assume that I cannot use an HTML element to trigger a touch event.)
You should probably a UIWebView. You can use an HTML anchor for touchable elements. The delegate will give you the option of doing something other than loading a web-page when the user touches the element. You can use a made-up URL format to uniquely identify each element.
Aside from that, you may want to use a custom control that draws all the text, rather than a series of UILabels. The UILabels will probably make it difficult to do line wrapping.

Break a text in a label in iphone

I am new to iphone development.I have created three labels in a cell and i have just displayed the text in the corresponding labels , but i want to display the full text in the label.I am not able to see the full text,So is there any possibility for breaking the text and show them in two lines.I am not displaying any static text .I am getting the text dynamically from the website and displaying it in the label.Please help me out.Thanks.
set label's numberOfLines property to 2 (or to 0 to have any number of lines you need)
set label's lineBreakMode property (e.g. to UILineBreakModeWordWrap)
make sure that label's height is enough to contain 2 (or more) lines of text