How to set UIButton titlelabel topdown - iphone

will like to know how to set the text in UIButton title Label in top down.
the text is "Press Me" across
will like to show
"
p
r
e
s
s
m
e
"
i did this
CGAffineTransform newTransform = CGAffineTransformMakeRotation(90 * (M_PI / 180));
self.activeButton.transform = newTransform;
but it only changed the button direction not the text

Rotating text to be vertical is different than writing each letter on a separate line, which is what I gathered to be your intention from your question.
To do that, you need to actually write each letter on a separate line! You can create new lines in UIButton text by holding Alt/Option while pressing enter in the text field in Interface Builder. Note that this has to be the text field property in the Utilities panel, you can't add new lines if you're editing the text by double clicking the button.
Once you've done this, change the "Line Break" mode to either Character Wrap or Word Wrap in order for it to display multiple lines.
Edit: I realised that you may be trying to work with your button in code, so I wrote this piece that should convert a regular button's text to be spaced vertically, letter by letter:
// Create a temporary NSString to store the new formatted text string
// Set it to the first character so we can have a simple loop from the second to the last character
NSString *newText = [NSString stringWithFormat:#"%C",[button.titleLabel.text characterAtIndex:0]];
for(int i=1;i<button.titleLabel.text.length;i++) {
// Format newText to include a newline and then the next character of the original string
newText = [NSString stringWithFormat:#"%#\n%C",newText,[button.titleLabel.text characterAtIndex:i]];
}
// We must change the word wrap mode of the button in order for text to display across multiple lines.
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
// .. and for an unknown reason, the text alignment needs to be reset. Replace this if you use something other than center alignment.
button.titleLabel.textAlignment = NSTextAlignmentCenter;
// newText now contains the properly formatted text string, so we can set this as the button label
[button setTitle:newText forState:UIControlStateNormal];

Related

Swift UILabel line spacing of break lines

I have a text that comes from multiple sources and ends with \n\n for a line break in UILabel that has lines set to 0 through Storyboard.
For example:
let text = “Some text at the beginning of the paragraph that is this long\n\nSecond type of text\n\nSome longer text that is on later on in the paragraph”
The output is correct:
Some text at the beginning of the paragraph that is this long
Second type of text
Some longer text that is on later on in the paragraph
I have changed the line spacing to slightly increase the gaps between lines but can’t change the height of the empty line. I want the line break to be approximately half the size of a normal line break. I tried after/before paragraph settings but can’t get an empty line to be half the size.
Any idea if this is possible and what is the best way to achieve this through storyboard or programmatically.
----- Edit:
This is what I tried:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 0.1
paragraphStyle.paragraphSpacingBefore = 0.1
let attrString = NSMutableAttributedString(string: text)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
cell.firstLabel?.attributedText = attrString

iText -- How do I get the rendered dimensions of text?

I would like to find out information about the layout of text in a PdfPCell. I'm aware of BaseFont.getWidthPointKerned(), but I'm looking for more detailed information like:
How many lines would a string need if rendered in a cell of a given width (say, 30pt)? What would the height in points of the PdfPCell be?
Give me the prefix or suffix of a string that fits in a cell of a given width and height. That is, if I have to render the text "Today is a good day to die" in a specific font in a PdfPCell of width 12pt and height 20pt, what portion of the string would fit in the available space?
Where does iText break a given string when asked to render it in a cell of a given width?
This is with regard to iText 2.1.6. Thanks.
iText uses the ColumnText class to render content to a cell. This is explained in my book on page 98-99. This means that, just like with ColumnText, you need to make the distinction between text mode and composite mode.
In any case, ColumnText measures the width of the characters and tests if they fit the available width. If not, the text is split. You can change the split behavior in different ways: by introducing hyphenation or by defining a custom split character.
I've written a small proof of concept to show how you could implement custom "truncation" behavior. See the TruncateTextInCell example.
Instead of adding the content to the cell, I have an empty cell for which I define a cell event. I pass the long text "D2 is a cell with more content than we can fit into the cell." to this event.
In the event, I use a fancy algorithm: I want the text to be truncated in the middle and insert "..." at the place where I truncated the text.
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 12);
float availableWidth = position.getWidth();
int contentLength = content.length();
int leftChar = 0;
int rightChar = contentLength - 1;
availableWidth -= bf.getWidthPoint("...", 12);
while (leftChar < contentLength && rightChar != leftChar) {
availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
if (availableWidth > 0)
leftChar++;
else
break;
availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
if (availableWidth > 0)
rightChar--;
else
break;
}
String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(position);
ct.addElement(new Paragraph(newContent, font));
ct.go();
As you can see, we get the available width from the position parameter and we check how many characters match, alternating between a character at the start and a character at the end of the content.
The result is shown in the resulting PDF: the content is truncated like this: "D2 is a c... the cell."
Your question about "how many lines" can be solved in a similar way. The ColumnText class has a getLinesWritten() method that gives you that information. You can find more info about positioning a ColumnText object in my answer to your other question: Can I tell iText how to clip text to fit in a cell

how to make content of NSString goes to next line based on \n on iphone

i have a NSString s=#"hi\nhello\n\nwelcome to this world\ni m jhon"
now how to display this content in UIlabel so that \n allow 1 line gap and \n\n allows 2 line gap
still i am getting same value
NSString *s=aDl.content;// aDl.content=hi\nhello\n\nwelcome to this world\ni m jhon
labelb.text=s;// its same without line break :(
kindly help me
Thanks
Set label's height big enough to accommodate all lines in your string. Then set label's numberOfLines property to 0 - so label will draw its text in as much lines as required. New lines symbols (\n) will be handled by label automatically:
NSString *s=#"hi\nhello\n\nwelcome to this world\ni m jhon"
label.frame = ...//big enough height
label.numberOfLines = 0;
label.text = s;

How to wrap a line in label in iphone?

I am new to iphone development .I want to wrap up the text in the label.I want to wrap up to a particular position in the label.Thanks.
theLabel.lineBreakMode = UILineBreakModeWordWrap;
theLabel.numberOfLines = 0;
That'll let it wrap an arbitrary number of lines. If you want to limit them, and have it truncate with a “...”, then set the numberOfLines property to that value.
Set "Line breaks" in Interface Builder ( http://drp.ly/d3K65 ) to "Word Wrap" and increase height of the UILabel.
You can do the same thing (or even more complex) in the code, see UILabel reference http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html

Wrap text in a UILabel with quotes?

By default, UILabels truncate text and then put an ellipsis on the end.
How might I wrap all the text, including the ellipse, in double quotes?
Use two UILables, the first holds the text (plus an open-quote), and the second just holds a close-quote:
["text that is lon…]["]
UILabel *label;
label.lineBreakMode = UILineBreakModeMiddleTruncation;
Unless there's an even better convenience method on the iPhone that I don't know about, I think the easiest and most flexible thing to do would be to subclass UILabel and implement your own drawing and truncation, using the various sizeWithFont extensions to determine the width of the string and each set of quotes individually.
Is your label text predictably going to result in truncation (and thus always have the ellipse)? I doubt it, but in case it does, you know the content is going to basically fill the width, so you can make the quote marks other UILabels (or even images). This would give you font and color control as well.
There is a correct way to do this, but it won't be the simplest thing ever. You need to do the following:
Determine the max height and max width of your label, with quotes. Determine the actual size of the label. You can use sizeWithFont:constrainedToSize:lineBreakMode: to do this. If the first is smaller than the second, strip the last word from your text, add ellipsis, and try again. That will look something like:
NSString *nextLine = rawTextWithoutQuotes;
NSRange range = [nextLine rangeOfString: #" " options: NSBackwardsSearch];
if (range.location == NSNotFound) {
return nextLine;
} else {
nextLine = [nextLine substringToIndex: range.location];
}
Keep doing this until you have manually truncated your string, then add the quotes and ellipsis, put it in your label, and you're done.