UITextView : Changing text colour modifies text position - iphone

I've got a UITextView with a small text in it. It displays correctly with black text on white background. However, when I want to change the text colour to a colour other than black, the text position changes (as if there was an empty line added at the beginning of the text).
With no colour change or with textView.textColor = [UIColor blackColor]; :
When I change to another colour, here textView.textColor = [UIColor blueColor]; :
The code line I've given really is the only code element that changes, and even if the colours in the background and the text are different, this is because they are generated randomly, in a complete other part of my code. The content of the text NEVER had any influence on the display before, and when I change the text colour it ALWAYS looks like in the 2nd picture.
I am stunned by this behaviour, as textColor shouldn't be modifying anything else than the font colour, should it ? Plus the fact that when I change to blackColor, no weird behaviour.
Thank you for your help...

"the different colours in the background are generated somewhere else" means
Where do you exactly change the colors?
You are changing some thing like
a)autoresizing the views (or)
b)Aligning text in that view (or)
c)Frame size of your textView can be smaller than the font size of your textview (or)
d)your text would be longer than actual size of the frame.
I would like to ask you one question.
Why do you use UITextView instead of UILabel??
I'm asking because textview will bring the scrollview, when the text is longer than your frame size.Probably that would be your problem I hope..

Related

Cannot change textcolor for Text in Unity

I need change Text color for UI Text to red, I tried on Indicator and script but the text color still have black color. What is wrong here ?
continueText.color = Red.color;
or
coreText.text = "<color="+textColor+">textContent"</color>";
or
Normally, Font include texture and metarial is packed. Here I make a editable copy of original font so that the metarial is unpacked. We need add metarial to Metarial box under color box choice to change color.
Thanks to #mgear for your help.

UILabel not wrapping correctly after text change

I'm working via the IB and have a UILabel that's stretched almost to the end of the layout. I have it set with Lines=2, because the max amount of lines it should take is 2. However if it's only 1 line long, I would like it to have a vertical justification of top.
Label settings in IB:
Lines:2
Line breaks: Word wrap
In code, in the viewDidLoad method I set the text of the UILabel. However as part of the functionality at a point the text must change. This is my code:
[_main_lbl1 setText:[myUI MAIN_TITLE]]; //Always only 1 line
[_main_lblsub1 setText:[myUI SUB_TITLE]]; //May be 1 or 2 lines
[_main_lblsub1 sizeToFit]; //Causes vertical alignment (I believe)
Whenever I change the text and rerun sizeToFit, the text wrapping becomes totally messed up. Instead of reaching almost the end of the UILabel as set up in the IB, in some cases the text will wrap at little more than half the distance, in some cases it doesn't wrap at all.
Image of layout in IB:
Image of resulting label in simulator:
In the first label it seems to be working ok, the second label doesn't wrap at all.
Is there anything I have to do to keep the text wrapping when changing the UILabel text? Anything else I'm missing?
Note: Updated question to include more detail and pics.
Thanks
The issue is that you're using sizeToFit. Which stretches the label out to fit the text. If you need to change the size you can use:
CGSize maxSize = CGSizeMake(320, 9999); // 999 means it can be as tall as you like
CGSize textSize = [label.text sizeWithFont:label.font
constrainedToSize:maxSize];
label.size = textSize;
You shouldn't have to do anything special. It will automatically wrap the text to fit when you change it. Otherwise the text would run out of the text labels bounds (which is not what you want). Your problem is that sizeToFit permanently changes the frame of the label. It makes it as small as possible while still showing the text. You are having it resize its frame to the original text and then you are changing the text so it is no longer sized properly. You should reset the frame back to it's original, change the text, and finally call size to fit again.
In viewDidLoad:
self.originalFrame = self.mainLabelSub1.frame;
Then in viewWillAppear:
self.mainLabel1.frame = self.originalFrame;
self.mainLabel1.text = #"New Text";
Note:
A good way to see the borders of the text label to get an idea for the wrapping potential is to temporary set the background of the label to something like magentaColor that stands out.

Why does the Label text not always start from the same point?

I have a UILabel which has 4 lines. Its font size is 15 & autoshrink is enabled. It's minimum font size is 10.
When my text is too long then it automatically starts from first line. But when my text is too small then it starts from middle line.
How can I make sure the text always starts from first line, whether it's too long or too short?
I think what you're looking for is more a UITextView than a UILabel.
From Apple's doc :
UITextView displays a region that can contain multiple lines of editable text.
The editable part is optional. You can disable it, along with scrolling if you want to be close to a UILabel.

Custom UITextField

How would I go about creating a UITextField like the one in this image?
It appears to be slightly larger, specifically in height.
This can be done much better and simpler with a stretchable image:
UIImage *fieldBGImage = [[UIImage imageNamed:#"input.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[myUITextField setBackground:fieldBGImage];
Think of the background of the text field as split into three sections. A middle section which can be stretched and caps on the ends. Create an image which only needs to be long enough to contain one pixel of this repeating section (a very short text field), and then create a stretchable image from it using stretchableImageWithLeftCapWidth: topCapHeight. Pass the width of the left end cap into 'leftCapWidth.' You can make it stretch vertically as well, but if your background image is the same height as your text box it wont have an effect.
If you're familiar with 9-slice scaling in Flash or Illustrator it's exactly the same concept, except the middle sections are only one pixel wide/tall.
The advantage of this is you don't have to worry about multiple layered objects scaling together and you can resize your text fields any time and the background will stay in tact. It works on other elements too!
You probably want to use the background and borderStyle properties of UITextField. Setting borderStyle as UITextBorderStyleNone and create a custom background image to be stretched and used as the background property would be one approach.
I suggest taking a look at those properties in the UITextField class reference.
You can do this by:
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"email-input.png"];
And if you want to give margin to your text inside the textfield, you can do this by:
// Setting Text Field Margins to display the user entered text Properly
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
yourTextField.leftView = paddingView;
yourTextField.leftViewMode = UITextFieldViewModeAlways;
You can also do this through Interface Builder.
just use the following line and it should work
textfield_name.background = [UIImage imageNamed : #"yourImage.png"];
here, "yourImage" is the background image you wanna set...
however, this will work only if your button isnt a roundrect button.So, you can change the type of the button in the Interface Builder or you can use
textfield_name.borderstyle = UITextBorderStyleNone or UITextBorderStyleBezel
and you r gud2go....!
Take an uiimageview set its image property to the image you want as uitextfield background. And on top of this uiimageview place an uitextfield with borderstyle none. This you can do directly in interface builder.

UILabel Question. Aligning overlapping UILabels

I have two UILabels. that I want to overlap one atop the other. Call the labels "under" and "over".
over: A C E G
under: B D F
UILabel "over" will have its text drawn in red. "under" will be in blue. The visual effect will be alternating colors between successive letters.
What are the controls available to me to exactly align the text in each label to pull this off?
Cheers,
Doug
I agree with fbrereton. This seems a very hard way to achieve the goal. Check out the NSString UIKit Additions to learn how to draw your own strings and lay the characters out yourself in your own custom -drawRect:. You'll have far greater control, and the code should not be that complex. iPhone doesn't have very good layout support (nothing like Mac offers), but for something this simple, it shouldn't be too bad.
You have to make sure the font you are using is a monospace font, otherwise the characters will not line up exactly as you would hope with just a space between them. (I believe there is a typewrite font available in the iPhone OS that is monospace; YMMV.) Also you will have to prefix underLabel with a space for the code below to work.
To map one UILabel on top of another, try:
overLabel.opaque = NO; // so you can see what is under overLabel
overLabel.textColor = [UIColor redColor];
overLabel.backgroundColor = [UIColor clearColor];
underLabel.frame = overLabel.frame;
underLabel.textColor = [UIColor blueColor];
Note in the above code underLabel takes on overLabel's frame because the latter's frame is wider; if it were the other way around overLabel would get clipped.
All that being said I'd wager there is a better way to skin this particular cat. This solution feels very "round peg, square hole" to me.