Button with three line text in iOS 10 Xamarin - ios

I am setting three line string title to UIButton. I am using iOS 10. But I can't set it.
What I have tried.
Set the UIButton title Label with the Below String.
String = "Before\n-\n12:00".
but the \n doesn't break the String to new line.
and also set the UIButton property to WordWrap.
When I set
noOfLine to TitleLabel of UIButton but noOfLine property is not display.
ScreenShot :
See the ScreenShot :
the First one is UIButton but it is not display well as I accepted.
the Second one is UILabel which is display well as I expected.
I want to set it like second one.
Is there any way to set it like Second One.
Any Help be Appreciated.

One Solution I found is that below
See the ScreenShot :
In the UIButton set the Title to Attributed
and also set the Title to centre.
The Output is As Expected.

Related

Is there a REAL way to change text color of selected date on UIDatePicker - Swift 3?

i spend 3 days searching and testing all these methods here:
1 - Change UIDatePicker TextColor in iOS10
2 - How to change the color of UIPickerView Selector
3 - Setting the text colour of a UIDatePicker in Swift
4 - Changing text color of datepicker
Some of them, help me to change the background color of the BG line (UIview):
sender.subviews[0].subviews[0].subviews[0].subviews[2].backgroundColor = COLOR_GREEN_LIGHT
This is good, could be better when i change the date it became white and then, when stop again, became green again. But it is ok.
The real problem is to try this solution for the UILabel subview with this array solution. Because, some array positions change, and ( for example ) month name, change the position ...subviews[0] to ...subviews[1].
Not great solution for that!
As it doesn't have a Delegate to work with it. I wondering, how to REAL solution to change text color only the SELECTED DATE??
Cause, the others question satisfy all picker text, not the selected.
Thanks for any light of help :)
You can conform to the UIPickerViewDelegate and implement pickerView(_:attributedTitleForRow:forComponent:).
Then you can return your title like so:
return NSAttributedString(string: "YOUR STRING HERE", attributes: [NSForegroundColorAttributeName: UIColor.green])
You can also add other attributes for your string like fonts.

iOS SDK: Difference between UIButton setTitleForState and UIButton titleLabel.text

I have this issue with a custom UIView where I have a UIButton subview, I want to set the button's text on initialization based on some condition like this:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=#"Some Title";
}
Nothing happens and the button's text is the same as defined in the nib file, however if I change the implementation to:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
[self.testButton setTitle:#"Some Title" forState:UIControlStateNormal];
}
It works as expected.
Can somebody please explain to me the difference between the two approaches? and when to use each?
EDIT:
the suggested answer doesn't explain my situation, I tested changing the button's text from another button's action like this:
- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=#"Some Title";
}
and the button's text changed. I just want to understand that behaviour.
Exact answer is
titleLabel
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has not
been displayed yet. The value of the property is nil for system
buttons.
setTitle
Use this method to set the title for the button. The title you specify
derives its formatting from the button’s associated label object. If
you set both a title and an attributed title for the button, the
button prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If a
title is not specified for a state, the default behavior is to use the
title associated with the UIControlStateNormal state. If the value for
UIControlStateNormal is not set, then the property defaults to a
system value.
Title label access is given to adjust other properties of the label such as font but setting titleLabel text does not work.
It is because UIButton class has inner implementation to set the text based on different states of the button like selected/highlighted etc which overrides label text.
The accepted answer is correct, I just add this here to elaborate a bit (comments are too short)
There's not much to explain here. The title is simply not meant to set the text at all. My guess is that the internal workings of UIButton make it save the text somewhere else as well (for different states). It uses a normal UILabel to eventually display that, because that's convenient and easy to understand. Setting the text of that does not change the button in all cases, which probably ultimately depends on the drawing cycle. I assume when it's drawn, laid out, or the like it "replaces" the label's text with its other saved variant at some point.
Now you might wonder why Apple did then expose the UILabel and thus seemed to make the text editable then.
Legacy is probably one aspect of this decision (IIRC you could once set the button's title that way). Although old code doesn't result in the desired behavior, it at least didn't crash immediately. Also, a lot of code simply wants to get the text and expects a label, that works perfectly fine as ever.
Second, designing it totally different seems overkill. To avoid that, they would have to use a subclass of UILabel which prevents you from setting the text or something and use that. Or skip it (and thus legacy support) completely and only offer the setTitle:forState: method. That seems like a bit much for a simple text container like a Label.
Ultimately it's a design choice made by Apple. You can't set the title text directly and there's no case in which you should do it any way other than by using setTitle:forState:

UITextField Alignment : Placeholder right aligned & Actual text gets left aligned

I have a UITextField set in xib. Text Alignment is set to right. When app is running, I see textfield which shows placeholder with right aligned text. When I start typing it still aligned to right. As soon as I move the cursor from that textfield to some other UI component, typed text gets left aligned.
EDIT :
I have resolved it partially in one scenario while editing in textField. But direct text assignment from local data source having same problem.
Editing Text Field Solution
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
Still facing alignment issue if I say,
textField.text = #"my Test";
Applied same logic before assigning text to textField as did in editing mode but still it gets left aligned automatically :(
Actual Output Problem
XIB Setting for UITextField
I have been using a UILabel category class for vertical alignment adjustment. In that there was a method +(void)loadand inside that method i was using following line
method_exchangeImplementations(class_getInstanceMethod(self, #selector(textRectForBounds:limitedToNumberOfLines:)), class_getInstanceMethod(self, #selector(verticalAlignTextRectForBounds:limitedToNumberOfLines:)));
method_exchangeImplementations(class_getInstanceMethod(self, #selector(drawTextInRect:)), class_getInstanceMethod(self, #selector(verticalAlignDrawTextInRect:)));
This particular lines of code caused the problem for me. It was overriding the existing default behaviour of the UITexfield. Commenting this worked for me.
So something similar would be there in your code also.
Hope this will help you :)
You can do the following:
1- set the textAligment to the right
2- add UITextFieldDelegate to your.h
3- yourTextField.delegate = self
4- Add the delegate method 'willBeginEditing' and set the textfield aligment to the left inside it
5- Add the delegate method 'didEndEditing' and set the textfield aligment to the right inside it IF the textfield.text is = #""
Happy Coding!
I have tried as same as you performed and it's working fine.
I think You have to delete that textfield and put it again with new identity.
Then Clean and Rebuild your project.
Make sure You are not giving any values or parameters to textfield programatically.

UIPickerView Separator lines

I am using Xcode 7.1.1 in my application I am using UIPickerView for selecting countries. It's working fine, I need two separator lines.
I tried to use this code:
[myPicker.subviews objectAtIndex:1].backgroundColor = [UIColor redColor];
[myPicker.subviews objectAtIndex:2].backgroundColor = [UIColor redColor];
but it showing line only in the bottom, in the top there is no line. Is there any way to show two lines between the bold text?
the myPicker.subviews you are trying to access is not something you know about. It does not contain what you think, that is, view or anything. Its apple private api and it can be anything. You cannot depend on it. It also depends on OS.
To make the selected text some sort of distinctly visible what you alternatively do is for all model objects create a corresponding UILabel in a loop and then use them to supply the data to the picker. Then in the
pickerView:didSelectRow:inComponent:
set some distinct property of your UILabel like change its textcolor or font.
add 1 pixel view in xib
hide both view & picker.
when you show picker..then display picker & 1 pixel view
when you click on cancel or Done remove your picker & 1 pixel view

Word Wrapping on UILabel iOS

Hi friends i have one requirement that word wrapping on label. Using myLabel.lineBreakMode=NSLineBreakByCharWrapping;
I am able to show the text in label like below
Designation : I am working as iOS
Developer
But i want to show
Designation : I am working as iOS
Developer
Is this possible to achieve above requirement within one label?
Thanks
I tried it and got it solved. I have attached the screenshot. You just need to do the following:
set lines property of label to 2.
set height and width of the label through xib and its done.
In Xcode, you can design your UILabel to have 2 lines. Then, you just need to add a new line ("\n") after the word "iOS", and center-align everything? If your caption is always the same you can pre-enter it in Xcode. To add a new line, just press alt+Enter.
Would that be acceptable?
No, it's not possible with UILabel, Unless workaround to this. I think, you know how to done manually. But You can't wrap the content like above. Simple answer is you can use two label.
Not possible. UILabel just renders text according to properties that you defined. If you want to do it ideally then use two UILabels. & Place them according to your need.

Resources