UITextView font to always be fixed size
Coding
var fontSize : CGFloat = 12.0 // DEFAULT SIZE
In your ViewDidLoad
if UIDevice().userInterfaceIdiom == .phone {
switch self.view.frame.size.height {
case 480:
fontSize = 12.0
case 568:
fontSize = 14.0
case 667:
fontSize = 16.0
case 736:
fontSize = 17.0
case 812:
fontSize = 18.0
default:
print("unknown")
}
}
else if UIDevice().userInterfaceIdiom == .pad {
fontSize = 20.0
}
Then apply this fontSize
, wherever u need.
Storyboard
Select UITextView, in Attributes Inspector
area, + (plus) symbol is near to Font
. By default it will work for iPhone
devices. For iPad
, Change change size classes to Regular width and Regular Height wR hR . For, that U can increase font size.
UITextView fix character width
As far as I know, Courier New
is the only fixed-width font shipped with iOS.
Also, you can always import custom fonts to your xcode project which you know are fixed-width. Here is how: Embed Custom Font in iOS
How do I size a UITextView to its content?
This works for both iOS 6.1 and iOS 7:
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
Or in Swift (Works with Swift 4.1 in iOS 11)
let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
If you want support for iOS 6.1 then you should also:
textview.scrollEnabled = NO;
Scaling UITextView And Font Size Change
The Affine Transform scaling factor is applied to text like anything else. So the onscreen text size should be the nominal textsize times the scaling factor. I.e., a 22px typeface zoomed by pinching to 1.25 scale would be 22 * 1.25 = 27.5px typeface.
Therefore, to keep the text size unchanged, you'd have to do the reverse: In your pinch recognizer, redraw the text in a typesize that is the nominal size DIVIDED by the scaling factor. Hence, draw 22px type at 22/1.25 = (about) 18px fontsize - which when scaled would be close to your original 22px size.
Clearly rounding is a potential issue, and you need to ensure the views of the text objects actually get redrawn. Does this help?
UITextView - setting font not working with iOS 6 on XCode 5
The issue was caused by the editable
property being false in the Storyboard. I have absolutely no idea why this caused the font to remain unchanged - and only on iOS 6.
iOS - i want to change UITextView Height from single row to some fixed hight when i enter the text in swift3
You can always increase the frame of the textView to a fixed height on editing start (textViewDidBeginEditing), if you really wish to have it fixed.
But that would mean if user fills it up again, it would stay that way.
You could also track editing changes (editingChanged) like this:
func textViewDidChange(_ textView: UITextView) {
let width = textView.frame.size.width
let height = CGFloat.greatestFiniteMagnitude
let textViewSize = textView.sizeThatFits(CGSize(width: width, height: height)
var textViewFrame = textView.frame
textViewFrame.size = CGSize(width: width, height: textViewFrame.height)
textView.frame = newFrame
}
Related Topics
Xcode & Swift - Window Without Title Bar But with Close, Minimize and Resize Buttons
Build Input File Cannot Be Found' Swift 4.2, Xcode 10.0
How to Do an If-Else Comparison on Enums with Arguments
How to Hide Status Bar and Navigation Bar When Tap Device
Difference Between Dispatchsourcetimer, Timer and Asyncafter
Xcode 6.3 Code Completion Too Slow
Difference Between Type Method and Type Instance Method, etc
How to Define Static Constant in a Class in Swift
How to Trigger an Action When a Swiftui Toggle() Is Toggled
How to Create Uicollectionviewcell Programmatically
How to Get the Current Queue Name in Swift 3
iOS 11 Large Title Navigation Bar Snaps Instead of Smooth Transition
Error: Binary Operator '<=' Cannot Be Applied to Operands of Type 'Int' and 'Int'
When Calling a Function in Swift, What Does Each Part Mean
Cross Platform Aes Encryption Between iOS and Kotlin/Java Using Apples Cryptokit
Is the Swift Divide "/" Operator Not Working or Have I Missed Something
In Swift, Does Int Have a Hidden Initializer That Takes a String