I am trying to use this method to draw a string into a custom UITableViewCell.
[self.text drawInRect:TEXT_RECT withFont:font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
The problem is that if the text is too long, the text is actually tail truncate but it doesn't display the "..."
If I use the drawInPoint
[self.text drawAtPoint:CGPointMake(60, 0) forWidth:200 withFont:font minFontSize:15 actualFontSize:nil lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
Then, I get the "..." but now it make all of my text one line, so the quite long text will get truncated too soon. For example:
If I have the text "Hello all, here is my first book". If I use drawInRect, then I can display it in 2 lines, but if I use drawAtPoint, I only see the first line like : "Hello all, here ..."
So, any help to make either method work will be appreciated
UILineBreakModeTailTruncation only truncates the last line of text. Is TEXT_RECT big enough to hold multiple lines of text? What appears to be tail truncation might just be a rect that isn't tall enough.
Related
I am using NSAttributedString (and NSMutableAttributeString) for writing to a PDF via UIGraphicsBeginPDFContextToFile. When I output an attributed string with underline attributes, the underline is broken when a letters descender goes over underline.
Here is a sample (screen capture) showing the current output:
And here is the code that builds that sample attributed string:
NSAttributedString* ftype =
[[NSMutableAttributedString alloc]
initWithString:#"Dangerous"
attributes:#{
NSParagraphStyleAttributeName:pstyle,
NSFontAttributeName:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:48.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]
}
];
My client's and my preference is that the underline be continuous, and ideally shifted below the descender.
Manually drawing the underline is difficult as we would have to computer the text position of the words after layout (sometimes the text is not as simple as the example above).
Does anyone have a fix to put the underline either a) lower or b) make it continuous?
Thanks in advance.
I know this was old and after searching found no answers. The solution I found is in the dictionary you pass in for attributes to NSAttributedString initWithString:attributes: add this
NSExpansionAttributeName : #-0.0001f
This scales the glyph, and it seems even with this small amount the underline doesn't get cut (well it didn't with the 13pt font I was using). Though this doesn't seem like the intended use of NSExpansionAttributeName, but couldn't find another solution.
I am programmatically writing to a UITextView a comma separated string.
Example: NSString* str = #"cat,dog,big dog,kitten".
On [textView setText:str]; I get the results as:
cat,dog,big
dog,kitten.
I don't understand why is the space getting converted to a line break. Any idea?
the space gets converted to a line break, because the text did not fit into one line. This line break mode cannot be disabled, or changed. But you can rise the width of the textview.
For some reason, UITextView produces an empty line at the end which causes it to be scrollable for nonsense when the text actually fits perfectly into the bounds.
I figured out that this additional space added to the bottom of the text in the contentView of UITextView is almost exactly the font size. So it is an additional line of text.
I want to remove that line so the UITextView is not scrollable when there is no reason to scroll.
editable is set to NO.
I tried setting the contentSize property with a reduced height but this has no effect.
The string has no whitespace or return character at the end. It comes from UITextView. Tested with a lot of different strings.
You can try altering the contentInset property and set the bottom inset to -10. I personally think it would be a hack but it worked when I tested it so just check what the correct inset would be.
textView.text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whiteSpaceAndNewLineCharacterSet]];
I have one line of text that is too long to be displayed in one single line in a UILabel, so I switched to a UITextView. I wanted to display that text still in one line, and be able to scroll horizontally, but instead, the UITextView will wrap the text into multiple lines, and allow me to scroll vertically. So, for the text "This is a very very long line", instead of displaying it like "This is a ", and then if scrolled horizontally, it would display the rest of it, it actually displays it like "This is a \n very very long \n line". How can I get the behavior I want?
Thanks,
Mihai
If you want that behavior you should place the label inside a scrollview. That's the only way you'll be able to scroll a label in any direction.
Using Merrimack's advice, this worked for me:
self.dishNameLabel.text = #"Here is some very longgggg text to test with";
float width = ceil([self.dishNameLabel.text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"WorkSans-SemiBold" size:15.0]}].width);
self.dishNameLabel.frame = CGRectMake(self.dishNameLabel.frame.origin.x, self.dishNameLabel.frame.origin.y, width, self.dishNameLabel.frame.size.height);
self.dishNameScrollView.contentSize = CGSizeMake( width, self.dishNameScrollView.frame.size.height);
Of course, you will have to replace your text and font with your respective specs
I display texts in a cell of the UITableView control. Now the texts that I display can be large enough to fit inside one cell width, so it displays the texts on the second line. But it breaks the word in the middle, (like if say "That sounds great, I will pick you up from the abc station", it will display "That sounds great, I will pick you up from the abc sta" on the first line and "tion" on the second line. However, I wanted to be like "That sounds great, I will pick you up from the abc" on the first line and "station" on the second line).
Could you let me know how can I do that?
Thanks.
I think you can achieve this by setting the lineBreakMode on the UILabel.
// setup cell 3.0 SDK code
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.text = #"this is some really long string....";