Curve UILabel dynamically? - swift

Is there a way / subclass to create a curved UILabel , and change it's angle dynamically, while keeping all it's properties ? ( like bending it to fit a circle)
The only example I found was to create it once, and it didn't really understood how to put it to work as a regular UILabel with all it's properties :
Draw text along circular path in Swift for iOS
I was looking for something like :
let label = UILabel ....
label.font...
label.text...
label.bendAngle ...

Related

Qt allignment of a QGraphicsSimpleTextItem in a QGraphicsScene

I want to add a simple text item (one word) to a graphics scene and so use QGraphicsSimpleTextItem. The scene coordinates of the text item defines the upper left corner of the text.
Is it possible to have the text center alligned around a coordinate?
Use setPos() for this. For example, where (X,Y) is the center point to which the text must be aligned:
QGraphicsSimpleTextItem *Item;
Item->setText( QString::fromLatin1( "My text" ) );
QRectF bR = Item->sceneBoundingRect();
Item->setPos( X - bR.width()/2, Y - bR.height()/2 );

Relative UILabel in a game object

I created a game object which acts as a repeating item for a UIGrid which I populate dynamically. The gameobject (RowItem) has couple of UILabel whose text can change on runtime depending on the content. The content of these UILabels overlap when the text is bigger. Can anybody help me in how to make UILabel expand relative to the adjacent UILabel when the text is more/less?
You can use transform.localScale property of the UILabel's property to scale it. Just make them bigger when the text is bigger than let's say 20 characters. Try with arbitrary values.
Also when you change the scale, run a re-align method, which aligns other labels so that they don't overlap.
you can get the text length in pixel by this:
UILabel label;
float width = label.relativeSize.x * label.transform.localScale.x;
float height = label.relativeSize.y * label.transform.localScale.y;
Let's say that you want to set you max length to 100, you can do this:
if (width > 100)
{
label.localScale = new Vector3(100 / label.relativeSize.x, 100/ label.relativeSize.x, 1);
}
the second param for Vector3 is also based on relativeSize.x is not a typo, that makes sure your text will not become thin.
Hope this works.

Find coordinates of a substring in UILabel

I have a string coming from server which I am displaying on UILabel. It is within that string, I am identifying some particular substring. I want to place a button on that substring(button will be a subview of UILabel). For this I require substring coordinates. I went through this Gist but I am not able to understand it.
Suppose my complete string is abc, 567-324-6554, New York. I want 567-324-6554 to be displayed on button, for which I need its coordinates. How can I use above link to find coordinates of substring?
Might be you can calculate using-
CGSize stringSize = [string sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
Now New position-
x = self.myLabel.frame.origin.x + stringSize.width
and for y similarly you need to have code with consideration of x.

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.