Here is what I am trying to achieve. Instagram notification cell style:
I have a UIButton, and 2 UILabels, each with separate styles. They are currently all individual, but I want them to look like one string. So no matter how many line breaks, label 1 follows the end of the UIButton title, and wraps around it if multiple lines. And label 2 follows the end of label 1 no matter how many line breaks.
I have looked into attributed text strings, only I got stuck on keeping the first part (currently UIButton) actionable.
And then I looked into stack views. But for the life of me, I can't see how I could have the text wrap around UIButton title text.
Also worth noting all text is dynamic.
Can someone please help me understand the best approach for this?
I'm using Swift 2.2
Related
I have two UILabels, both will have the dynamic text but interrelated.
for example:
label1.text=#"Abc";
label2.text=#"Meaning of Abc is ......";
Now label2 will always have label1.text plus some extra text.
I want to make this part in label2 to be in Bold.
So far I found that it could be done by drawing the text in drawRect but that seems to be very complicated.
Is there any other way out is possible??
Thanks
Ben
iOS 6 will supported attributed label for this sort of thing. Until then or if iOS 5=< compatibility is important to you, you can make the label a UIWebView and use HTML markup. Or do it the hard way with drawRect (which will perform better).
Edit: There are third party libraries that add attributed labels, Nimbus is one of them that works well.
I have this problem with one of my UILabels, I would like to have the text that it displays begin being drawn at the top left of the label rather than the middle left.
Current screen:
As you can see the word "Description" in the label appears in the centre and to the left of the label. I would like it to start at the top-left instead. How do I implement this either programatically or in IB?
Thanks,
Jack
I'd assume that you're eventually going to put a description in, in which case it would be easier in the long run to simply make the UILabel for "Description" one line high, and put it at the top. Then, just add another multiline UILabel for the actual content. Having separate UILabels will make things simpler later.
Changing the Description label to a UITextView pretty much solved my problem. It allowed for multiple lines of text and was much simpler to implement. Also looked a little prettier.
Thanks for your help, particularly Daniel.
Jack
I've a UITextView, its quiet big, I sized it so it could fit 4 lines of text, so if the user wants to write a long note it can be read while its being written.
The problem is that text stays on the top line and it scrolls horizontally rather than wrapping around and dropping down to a line below it. Like you see when you write a text message on your phone.
Is there anything that can be done to get a UITextField to act like this? Or am I required to use an editable UITextView instead?
Looking at the docs it would seem UITextView cant provide the functionality I need.
Many Thanks
-Code
HI,
I do'nt think , It could be possible by using UITextField (support single line),You will have to use the UITextView (for multiline text) .
See the below tutorial , It grow at runtime while the user type the text using keyboard and can expend till certain line ...
http://www.hanspinckaers.com/multi-line-uitextview-similar-to-sms
If it is an IBOutlet than you can just enable vertical scroll and remove horizontal scroll from scrollers.
Hope this helps.
I have an app which shows text in a UIPickerView based on a search. I want to highlight specific letters in the string that are there as a result of a wildcard character.
For example, if I searched for "CA?", one of the rows will show "CAT" and I want only the letter "T" to be in the color blue.
Any ideas? The user gets immediate feedback as he types so performance is important.
To create a string that has different font properties for different characters, you would generally use NSAttributedString. However, UIPickerView doesn't seem to directly support using NSAttributedStrings as the labels for your picker components, nor does UILabel seem to support them. You might have to create a custom UIView subclass and return it from pickerView:viewForRow:forComponent:reusingView: in your UIPickerViewDelegate.
Thanks to David for the tip to get me started.
I ended up using the Three20 library and returning a TTStyledTextLabel from pickerView:viewForRow:forComponent:reusingView: with the text property set to [TTStyledText textFromXHTML:myXHTML] along with a TTDefaultStyleSheet to define the colored spans. Works great and seems to be very fast in the UIPickerView component.
I have spent a few days trying to get around the limitations with UITextField, namely no text wrap and number of lines. I have created a UILabel, which is used to display the text entered in UITextField and does all the formatting stuff properly. The UITextField is hidden and the user sees all the text entered only in UILabel as it's being entered.
Everything is working perfectly except for the lack of a cursor on the UILabel to show the user where the next character typed into the field will show up.
I have experimented with using various characters as cursors on the label. But there is no getting around the fact that it is not the standard blinking cursor indicator on the iPhone and so the whole thing just looks wrong.
Before I abandon ship and go for a UITextView (with its own set of issues) I was wondering if anyone has any ideas as to how a blinking cursor can be added to the text field on a label text.
Thanks in advance.
Your approach has other issues which make it worth rethinking the strategy.
How does selection look like?
copy + paste?
Do you handle right to left languages?
Auto correction?
The list is certainly longer, but I think it's enough to consider other solutions. But I agree that all of UIKit's text handling is a bit poor.
If you don't need to support selection, copy and paste, and only need multiline input, you could use a | character and animate it as if it were blinking... either that or perhaps a custom overlay view on top of the label, that would implement the cursor drawing, animation and positioning based on the length of the string and the font used.
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
may help to achieve just that.
So what are the issues preventing you from going to a UITextView? It seems possibly easier to address those.