dynamic textview with arabic text entry - ios

I am using a dynamic textview.
func textViewDidChange(_ textView: UITextView) {
let str = textView.text! as NSString
let size = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20.0)])
textView.frame = CGRect(origin: textView.frame.origin, size: size)
}
This is the code used for dynamic change in height and width. This works well while using "English". The problem is when i change language to "arabic", text view has to increase width towards the left, but it still increasing the width towards the right.

First check the iPhone screen co-ordinate system below.
I am saying if you increase the width it will always increasing the width towards the right in iPhone. So set your initial origin of the UITextView at the right most and then decrease origin's X Co-ordinate by the amount 'size' you calculate and then set the new origin and size values to the UITextView frame. Hope u understand.
For example :
lets say your initial UITextView origin is at (300,30) and size is (30,30)
now you enter some text Arabic text, and you calculate the size of the text as 100.
Now your new origin's X will be 300 - 100 = 200
so newOrigin = (200,30)
and newSize = (100,30)
now set the UITextView frame as- textView.frame = CGRect(origin: newOrigin, size: newSize)

Related

Increase font size based on the iphone screen size [duplicate]

This question already has answers here:
How can we keep a text field's font size relative to the screen size?
(2 answers)
Closed 5 years ago.
How to change font sizes automatically of all buttons, label title based on the devices (iphone/ipad) in the UIStoryboard? If i change in programmatically there are my different screen sizes how can i change for all the components. Is there any way to do that in proper way ?
Try This For Swift
Create function.
func setCustomFont() -> CGFloat {
//Current runable device/simulator width find
let bounds = UIScreen.main.bounds
let width = bounds.size.width
// basewidth you have set like your base storybord is IPhoneSE this storybord width 320px.
let baseWidth: CGFloat = 320
// "14" font size is defult font size
let fontSize = 14 * (width / baseWidth)
return fontSize
}
Use this function.
yourbutton.titleLabel?.font = UIFont.init(name: "Helvetica", size: setCustomFont())
This type you also set UILabel and UITextfield font but this not use for UIButton.
1.Go to storyboard select UILabel/UITextfiled.
2.Go in Attribute Inspector
3.Check Dynamic Type option Automatically Adjust Font.
If you are using the interface builder you can set separate size classes for the elements and adjust accordingly
You can use AutoShrink like this:
yourButton.titleLabel.minimumScaleFactor = 0.5;
yourButton.titleLabel.adjustsFontSizeToFitWidth = YES;
This won't adjust the font size bigger than we set.But it will increase based on phone size upto maximum size that we set.

iOS Swift UITextfield adjust fontsize width

How to adjust UITextfield font size when it is hold long character?
I tried adjustsFontSizeToFitWidth. it is not working.
any help will be appricated.thanks in advance
With a UITextField, text must fit on one line and cannot wrap.
You have two options:
Shrink the font to fit on one line:
self.TextFieldExample.adjustsFontSizeToFitWidth = YES;
self.TextFieldExample.minimumFontSize = 10.0; //Optionally specify min size
Use UITextView to enable text wrapping:
Working Swift 4 Solution
Make sure you set adjustsFontSizeToFitWidth to true and then set your minimumFontSize. This will scale the text inside the UITextField to fit the width of your UITextField with a minimum font size equal to or greater than what you set minimumFontSize to. When the text is still too long to scale down to fit at the minimum font size it will truncate the rest.
textField.adjustsFontSizeToFitWidth = true
textField.minimumFontSize = 10
You have to set the width first
textField.frame = CGRect(0, 0, your width, your height)
And
textField.minimumFontSize = 0.5

UILabel with background view + padding that follows text layout

I am trying to achieve a pretty complicated layout in my UICollectionViewCells where I have a UILabel that should have a white background with padding + 2px corner radius, that should follow the text.
If the text is too long it should be two lines, and the background should contain its padding. It's pretty hard to explain, but I think the picture will tell more.
Let me know if you have any solutions or suggestions.
Put the label inside uiview and give it a white background and play with its constraints and background colour.
You could proceed as follow :
Check the size of your string
Compare it with the size of your box
If the text size is bigger than the size of the box, you increase its size
//Get your textfield size
var frameRect = textField.frame;
let myString: NSString = textField.text! as NSString
//Get the size of your string
let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
//If your text doesn't fit, increase the size of your box
if size.width > frameRect.width {
frameRect.size.height = 50; // <-- Specify the height you want here.
textField.frame = frameRect;
}
Tell me if this works

uitextview not scrollable with autoresizing font to fill it with any text length

I would like to make a not sliding UITextView with automatic resizing of character to fill the textview and see all the text regardless of it's length.
in some post say to compare the height of the content and that of the frame to determine the font size, for example:
if (self.textview.contentSize.height > self.textview.frame.size.height {
int fontIncrement = 1;
while (self.textview.contentSize.height > self.textview.frame.size.height) {
self.textview.font = [UIFont systemFontOfSize:FONT_SIZE - fontIncrement];
fontIncrement++;
}
}
and these two lines to determine the size of the content:
CGSize maximumLabelSize = self.textview.frame.size;
CGSize expectSize = [self.textview sizeThatFits:maximumLabelSize];
but i have many problems for apply this o similar solutions, for example:
in the first time, by printing frame and content heights on the console they are set to 128 instead of 350 (size of storyboard and simulator). If i change the text with some one on execution, nslog print the correct value 350 but is always the same for frame and content.
the frame and the content are the same height with any text.
what's my problem? there is a way to do this Correct?

Get UILabel font pointsize after minimumFontSize

I have a UILabel with a font of point size 17. If I call label.font.pointSize I get 17, which is all good. BBUUUUTTT I also have a minimumfontsize set to 8, now if I cram some text in the label which causes the point size to shrink and then call label.font.pointsize I still get 17 even though I know the point size is smaller
Any ideas how to get the true point size after system has resized the font?
I don't know of an API to get the current point size of the UILabel when it is scaling your content down. You can try to approximate "scaling factor" using sizeWithFont APIs.
Just an idea:
// Get the size of the text with no scaling (one line)
CGSize sizeOneLine = [label.text sizeWithFont:label.font];
// Get the size of the text enforcing the scaling based on label width
CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size];
// Approximate scaling factor
CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width;
// Approximate new point size
CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize;
As savner pointed out in the comments, this is a duplication question. The cleanest solution is found here: How to get UILabel (UITextView) auto adjusted font size?. However, Sanjit's solution also works! Thanks Everybody!
CGFloat actualFontSize;
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Swift 4 and iOS 7+ version (sizeWithFont is now deprecated) of #Sanjit Saluja's answer:
// Get the size of the text with no scaling (one line)
let sizeOneLine: CGSize = label.text!.size(withAttributes: [NSAttributedStringKey.font: label.font])
// Get the size of the text enforcing the scaling based on label width
let sizeOneLineConstrained: CGSize = label.text!.boundingRect(with: label.frame.size, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil).size
// Approximate scaling factor
let approxScaleFactor: CGFloat = sizeOneLineConstrained.width / sizeOneLine.width
// Approximate new point size
let approxScaledPointSize: CGFloat = approxScaleFactor * label.font.pointSize

Resources