UItextField with Multiple lines - ios

I am trying to make a form and the requirement is to have a UItextfield with multiple lines which means, when a specific number of characters are entered in one line the blip moves to next line also on hitting enter it should enter the next line as well.
Currently, I am working with Xcode 9.4 but it offers a single line text field.

I used UITextView with this code:
lazy var commentTextView: UITextView = {
// Create a TextView.
let textView: UITextView = UITextView()
// Round the corners.
textView.layer.masksToBounds = true
// Set the size of the roundness.
textView.layer.cornerRadius = 20.0
// Set the thickness of the border.
textView.layer.borderWidth = 1
// Set the border color to black.
textView.layer.borderColor = UIColor.systemGray.cgColor
// Set the font.
textView.font = UIFont.systemFont(ofSize: 16.0)
// Set font color.
textView.textColor = UIColor.black
// Set left justified.
textView.textAlignment = NSTextAlignment.left
// Automatically detect links, dates, etc. and convert them to links.
textView.dataDetectorTypes = UIDataDetectorTypes.all
// Set shadow darkness.
textView.layer.shadowOpacity = 0.5
// Make text uneditable.
textView.isEditable = true
return textView
}()
and this is the result

You should use UITextView for multiline string or there is third party library which you can use.
MultilineTextField

Related

Round Icon Badge using CALayer and CATextLayer On UIBUtton

[![enter image description here][1]][1]Can any one share their code relating to show round Badge number icon on UIButton on Top right Corner Using CALayer And CATextLayer and Add to UIButton
Thanks In advance
For your badge, you can use only label for showing the badge on button specially.
let label = UILabel()
label.clipsToBounds = true
label.layer.cornerRadius = label.font.pointSize * 1.2 / 2
label.backgroundColor = UIColor.grayColor()
label.textColor = UIColor.whiteColor()
label.text = " Some Text "; // note spaces before and after text
And For reusability, you can create the category or extension for this code.

How to add background color and rounded corners for Strings in iOS?

How to add background color and rounded corners for Strings in iOS?
like this picture below:
Can't do it with NSAttributedstring. So, should I use CoreText? I found this answer but still don't know how to do it.
String is just a data in text format, it's not visual. You need labels, those are labels there. And then set your string to that label.
let label = UILabel()
label.backgroundColor = .green
label.text = "Label" // You set your string to your label
label.layer.cornerRadius = 5
label.layer.borderColor = .clear
label.layer.borderWidth = 1
label.layer.clipsToBounds = true
Code above creates a label and sets everything you need to achieve it like in the picture. Now you need to add this label into your view or you can set these properties to an existing label you got.
EDIT:
If you want to make individual text inside a text view colorful, you can use TTTAttributedLabel and set an attributed text with background to individual pieces of text.
You can find TTTAttributedLabel here.
CornerRadius: kTTTBackgroundCornerRadiusAttributeName
BackgroundColor: kTTTBackgroundFillColorAttributeName
Example usage:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:TTTAttributedTestString()];
[string addAttribute:kTTTBackgroundFillColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [string length])];

How to get actual width of UILabel text when set to word wrap

I have the following code to set up a UILabel:
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .center
label.lineBreakMode = .byWordWrapping
label.preferredMaxLayoutWidth = maxWidth
label.text = "String that is long enough to cause word wrap."
label.sizeToFit()
self.view.addSubview(label)
The result that I need is for the label to wrap the text as it would once it reaches label.preferredMaxLayoutWidth then actually size itself to just wide enough to fit the wrapped text. Here's a diagram to illustrate:
The red box's width is equal to label.preferredMaxLayoutWidth and that is the actual size I am getting with my code above.
The black box is the size that I would like it to be.
Whether the label was sized to the red or black, the word wrapping would be the same.
So, how can I resize my label to be the smallest possible width while maintaining the current word wrap?
Edit:
Although constraints might be the usual option, in my case, I don't think it is because I am sizing my superview according to the resulting size of this label.
Ok, I just figured it out. #rmaddy was correct in the question's comments above. I had seen boundingRectWithSize:options:attributes:context: before asking the question, but had issues figuring it out in swift. But now I've got it. Here is my updated code:
let label = UILabel()
let text = "String that is long enough to cause word wrap."
let rect = text.boundingRect(with: CGSize(width: maxDesiredWidth, height:CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: nil, context: nil)
label.numberOfLines = 0
label.textAlignment = .center
label.lineBreakMode = .byWordWrapping
label.frame = rect
label.text = text
label.sizeToFit()
self.view.addSubview(label)

How to add image and text in UITextField as placeholder in center in swift

I am new to Swift so bear with me I want to add an image and text as horizontal align centered in the UITextField. Using following code I was able to add image in the textbox but it is in center and also it remains there when textbox gets focus. I want it in center with placeholder text and when UITextField gets focus it hides.
var imageView = UIImageView()
var image = UIImage(named: "all.png")
imageView.image = image
searchFiled.leftView = imageView
Unfortunately I do not 'speak' Swift, but it should be easily possible to translate the Objective-C version...
Your talking about the so-called 'placeholder'; the string (or attributed string), that is shown in the UITextField, when no really text is inserted.
To show an image here use the following code:
// Create a NSTextAttachment with your image
NSTextAttachment* placeholderImageTextAttachment = [[NSTextAttachment alloc] init];
placeholderImageTextAttachment.image = [UIImage imageNamed:#"Your image name"];
// Use 'bound' to adjust position and size
placeholderImageTextAttachment.bounds = CGRectMake(0, 0, 16, 16);
NSMutableAttributedString* placeholderImageString = [[NSAttributedString attributedStringWithAttachment:placeholderImageTextAttachment] mutableCopy];
// Append the placeholder text
NSMutableAttributedString* placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(#"Search", nil)];
[placeholderImageString appendAttributedString:placeholderString];
// set as (attributed) placeholder
_yourTextField.attributedPlaceholder = placeholderImageString;
If by focus, you mean the user clicks on the UITextField, then what you want is to have your view controller act as a UITextFieldDelegate. You want to implement the optional func textFieldDidBeginEditing(_ textField: UITextField) function. In that function you want to hide the image.

iOS UIButton with multiple labels

I have a UI button that I'd like to put two labels on it, similar to how a cell has a title text and detail text.
I'd like the button to have a larger font for the main text, and have smaller detail text under that.
Is this possible? I've tried to put multiple lines on a button, but I need to have different text sizes for each line, so setting the lineBreakMode and numberOfLines of the titleLabel doesn't really quite work.
Here's the code we finally used. Assistance from John Wang.
Thanks to everyone for the suggestions!
// Formats a label to add to a button. Supports multiline buttons
// Parameters:
// button - the button to add the label to
// height - height of the label. usual value is 44
// offset - the offset from the top of the button
// labelText - the text for the label
// color - color of the text
// formatAsBold - YES = bold NO = normal weight
// tagNumber - tag for the label
- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber {
// Get width of button
double buttonWidth= button.frame.size.width;
// Initialize buttonLabel
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)];
// Set font size and weight of label
if (formatAsBold) {
buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize];
}
else {
buttonLabel.font = [UIFont systemFontOfSize:fontSize];
}
// set font color of label
buttonLabel.textColor = color;
// Set background color, text, tag, and font
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.text = labelText;
buttonLabel.tag = tagNumber;
// Center label
buttonLabel.textAlignment = UITextAlignmentCenter;
// Add label to button
[button addSubview:buttonLabel];
[buttonLabel autorelease];
} // End formatLabelForButton
A trick I would recommend is putting a UIButton with a transparent interior on top of UILabels. I've used this trick before and, although it may present some problems in terms of maintenance and i18n, it works like a charm.
Here is a 5 minutes sample using the suggestion above.
Given more time, you can make a better label with round corners.
you should be able to add subviews to it. Since everything is a view, everything can potentially have subviews.
I would subclass it and put the labels on it within the subclass, Then you can extend properties for text and subtext to change their values.
Not saying it can 100% work. But off the top of my head. UIView can have SubViews

Resources