iOS + UIlabel with Start and end Quotes in UITableViewCell - ios

I'm trying to create a UITableViewCell like the one in picture. It has a UILabel and at the start and end Start and endQuotes. I have already looked at NSAttributedText string but it looks horrible when quotes are included in text. Do let me know if anyone has ideas

One idea is to make sure the text starts at the specified distance to the right, e.g. by inserting spaces. Then you would have to find out the x position of the last character - this should also be possible. The quotes could be images that you then superimpose.
For finding the x of the last line, you could calculate the total height, figure out how much of the text fits if you remove one line, then calculate the width of the remaining text.
The new boundingRectWithSize:options:attributes:context: should be enough to make these calculations.

Related

FPDF/FPDI: How to vertically align a cell or multi-cell?

I'm starting to think is not possible? (out of the box/default class)
I am using FPDF/FPDI PHP class to generate a .pdf
In this situation, I have some text that can be short, or be long (no telling)
So I set a width on my cell.. and change from Cell() to MultiCell()..
This allows long text to wrap.. however it seems to vertically align to the top?
Which is no good if the string is NOT long enough to wrap..
How can you force the text to the bottom of the cell regardless if it is a single line or a wrapped line?
Is there a default way to do this?
I saw mention of TCPDF (or whatever).. and some dead links to 'plug-ins'.. (but not sure if they were FPDI or not?)
This works and looks fine:
$pdf->MultiCell(185, 12, 'ABC-123-DEF-456 And-Last-Name-Here', 1, 'C', false);
However this way, does NOT have the (single line) text at the bottom.. so there is a cap between the string output and the 'underline' it shoudl match up with with (baseline)
$pdf->MultiCell(185, 12, 'ABC-123 DEF-456', 1, 'C', false);
How do you overcome this?
I think, I just found solution.
As default FPDF class I use this code: https://github.com/Setasign/tFPDF/blob/master/tfpdf.php (it supports UTF-8 chars)
Than have edited this class code: http://www.fpdf.org/en/script/script52.php
In Function drawRows() I have changed this row:
$l+=$cw[$c];
To this:
$l+=$this->GetStringWidth($c)/$this->FontSize*1000;
And than use drawTextBox() as is descripted in http://www.fpdf.org/en/script/script52.php
Now I will try to explain:
GetStringWidth() function supports utf-8 chars soo we can use it to calculate char width. And than we can get correct value with some multiplications and divisions for compability with that fpdf class.
I didnt test this code extensivly... only basic testing.
You can do it with a new line, "\n". But you need to keep in mind that cell height needs to be determined by number of "\n", as multi_cell will increase cell height automatically when required.
This is how I have done it:
spacing = " \n\n\n\n\n\n\n\n\n"
contents = "\n" + "hello" + spacing
pdf.multi_cell(10, 3, contents, border=1, align="R")
This will align "hello" to the top right corner.
When creating the spacing I added a space before the \n's otherwise it will be very close to the edge. I also added a \n before the "hello" as otherwise it will be too close to the top. I kept the multi_cell height to 3, but you can even put it to 1 or anything that makes sure your size of \n's is not smaller than the cell height.
I know this is very gimmicky, so if you have a better way please let me know.
This seems could be the answer:
http://www.fpdf.org/en/script/script52.php
Unfortunately, at least to me, it doesn't seem to work when using UTF-8 characters.

Find substring that would fit inside UILabel of certain area

For a UILabel of certain width, with certain font and font size, i want to calculate amount of characters that would make 7 lines worth of text inside it + ... (three dots showing continuation). Is there a fancier way to achieve this? Currently what i'm trying is counting up to X amount of characters or 7 new line characters, which ever comes first and i cut on the text right there.
More Detail:
Trying to make an expandable row Cell which contains the UILabel, i'm achieving this with auto layout... So to control the cell expansion, i change the text to be full text or a substring of that, with a button below which toggles between the string vs substring. All of that is working. The problem i'm getting is my method of finding the substring isnt very neat. Its not consistent on how it handles text of different combination of characters or newlines. I get variations of how it looks and sometimes it just ends with three dots on a new line rather than finishing on the 7th line.
Even using auto layout you can still use the lines property of UILabel to limit the number of lines displayed by the label.
So set it 7 and you'll get 7 or less rows. Just assign the complete text.

Fitting multi-line text into a dynamically size-changing node

A multiline auto typing text box class (which uses an SKNode as the parent) is created using basically 2 elements:
an SKSpriteNode that acts as text box frame & background image/texture holder.
an NSMutableArray containing a set limited amount (rows) of NSStrings that each have a set character length.
After modifying this text box class so that it can be initialized with any frame width & height, I realized I didn't program the NSMutableArray to automatically change its content in a such way that it nicely fits within the background node (with a bit of padding involved as well). So here I am wondering how to do that since NSString's can only return the character count and not the width & height of each string in points (points could have maybe helped me create character constraints in some way).
Right now, the NSMutableArray uses a hardcoded maximum character count per NSString & a maximum row count for the entire array (it's 5 rows right now and when that limit is reached, a new "page"/array is created). This forces me to manually re-adjust these parameters every time I change the background node frame size which defeats the purpose of the class allowing the background frame to change.
Thing is, I'm trying to solve this in such a way that when I post this class on github, I want the solution to take into consideration any fontName & fontSize.
What are my options for solving this problem?
I've done something similar to this. It doesn't work 100% as to what you want, but should be similar enough. It uses a root node and from there, it will build multi-line text using an array of NSString which will in turn be used to build the SKLabelNode.
I'll outline what I did. I should also say I only run this when new text is set. In other words, I do not incur the penalty of deriving the information every frame. Only once.
The generalized steps are:
You will iterate over each character in the text string. Note I do this because my code supports word wrapping as well as other alignment capabilities. So for me, I want that level of control. As this is being done only upon creation, I'm fine with the overhead. If you don't want to word wrap you could always just create an array of words and work from there.
As you iterate over each character, you'll be generating an array of lines. Where each line in the array is a line that will fit in your frame. For now let's not worry about vertical constraints. So here we are primarily worried about width. For the current line, each character you are iterating over will get added to the current line. For this potential line string, you will use NSString's sizeWithAttributes, which is configured for your font. For example in my code it is an NSDictionary which contains: NSFontAttributeName : [UIFont fontWithName:self.fontName size:self.size]. This will be used to check the width, if that width exceeds the frame width, you are overrunning the line.
So the code may look something like:
size = [line sizeWithAttributes:attributes];
if (size.width > maxTextWidth) {
needNewline = YES;
}
If you have overrun a line, you need to determine if you are word wrapping. If you are, you can just add the current line (minus one character) to the lines array. If not you have prune off the last word in the current line and then add that to the array of lines.
The tricky parts are dealing with whitespace and handling non-word wrapped overflow. I have not addressed whitespace but you need to consider this very much in your code. Additionally, you also do want to factor in leading pixels, etc.
Once you have your array of lines, you can then create your children SKLabelNodes. I add them to the root, which allows me to move the group anywhere it needs to be.
The real key here is the lines array generation.

Calculate characters that fit a fixed rect TextView

I have seen here people needing to calculate the size of the NSString given a size but I need to do the opposite.
Given a specified rect (or fixed UITextView, or multiline UILabel, no scrolling) I need to know:
if it managed to show all the chars of my NSString
if not, what is the last char shown
So that I can display the remaining text in another UITextView (of course if I could use a single UITextView I would not have this problem).
At first it seems a simple thing to do, but actually I am not finding a way, intuitively I think I could use either UITextView's:
textView.contentSize.height;
or NSString's:
sizeWithFont:constrainedToSize:lineBreakMode:
or a combination of the two, but I need to be precise and those methods do not help me in telling what is the last character that managed to fit the visible area of the UITextView.
Not sure if this is actually possible, but is a requirement of my client who thinks programming iOS is like printing a newspaper and expects to be able to format text around an image....
You could maybe get the maximum height of one line of text from a one character long string.
If you use that with sizeWithFont:constrainedToSize:lineBreakMode: then you should be able to know if your text runs onto more than one line (if the cgsize height is greater than the height of one line of text).
In order to find out the last character (or word) you would have to loop around the length of the string adding characters (or words) as you go and checking for when the cgsize height increases to add a new line, this will give you the character point when to split into multiple strings ( for multiple fields/labels/textviews ) or when to insert line breaks into the text ( if using a single multi-line textview or label ).
I hope you find an easier way...

Align UITextView Right and Left.

I have UITextView, which is left aligned.
When last word does not fit on current line it goes to next line leaving spaces on end of line.
which does not give good look and feel.
So, what I want that if words of particular line feels the spaces left at end.
i.e. Spaces between two words can dynamically varies.
Here I am giving example of Scenario:
The width of text view,never put off until (here tomorrow does not fit,so it goes to next line leaving spaces).
Tomorrow what you can avoid all together.
So, problem is it does look good.
What I want is:
The width of text view never put off, until (varying spaces shown by)
tomorrow -what -you -can
avoid --all -- together.
Thanks in Advance.
There is no setting for justified aligment for text you only have center, left and right. If you want justified aligment where every line has the same amount of characters or something like that you can format the text entered to have carriage returns after x amount of characters or words, or something like that.

Resources