UITextView font is nil - ios

I created a UITextView with a font size of 14 in a storyboard and hooked it up to the detailDescriptionLabel property of my ViewController. This code is in viewDidLoad:
self.detailDescriptionLabel.font=[UIFont systemFontOfSize:10];
NSLog(#"text is %#; font is %#", self.detailDescriptionLabel.text, self.detailDescriptionLabel.font);
The console output follows:
text is Lorem Ipsum; font is (null)
Why is the font set to nil? The setFont: is working; the font does shrink. I want to get the font so after a gesture I can call lineHeight on the font. This way, I can find out which line has been tapped with the following code:
int line = [tap locationInView:tap.view].y / self.detailDescriptionLabel.font.lineHeight;
Here, too, the font is nil. line is set to -2147483648, which is obviously not what I want.

Try checking the "selectable" checkbox for this UITextView in Interface Builder. It's in the Attributes Inspector. Per #VahramDodoryan's comment below, you can then set selectable to false if you don't want to support selection.
I can't explain why this works, but it's probably a UIKit bug. I had an IBOutlet to a UITextView whose font property was nil, and it would not respond to any font or text-color changes in code until after its text property had been set. I arrived at this solution through trial-and-error.
If you're still encountering this issue on recent releases of iOS, consider opening a radar:
https://openradar.appspot.com/15854592
https://openradar.appspot.com/15196932

I tried changing every property and constraint , I had on my text view.
Including selectable as suggested above
In frustration in the end I deleted and re-added it and that was the unsatisfying fix.

You should try logging self.detailDescriptionLabel. The value might be null.

Related

Button with three line text in iOS 10 Xamarin

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.

UITextField font-size when content and losing focus

I have a UITextField for which I would like to assign a custom font and font-size like this
self.txtEmail.font = UIFont("HelveticaNeue-Light", size: 24)
When I start the app everything looks good. My placeholder text gets the new font and if I click on the input field and start typing, the text has the new font. However, when I click somewhere else and the input field lose focus the font is reset to the default font. What I find even more strange is that I only have set this default font for UILabels and not UITextField by adding this line in the application method inside AppDelegate. If I try to set the default font for UITextField nothing happens.
UILabel.appearance().font = UIFont(name: "HelveticeNeue", size: 18)
So my question is why I get the behavior where the UITextField has the correct font until it gets content and lose focus.
I have 1 general remark on UI elements' appearance management and one more on UITextField.
A) Appearance:
The documentation says:
To customize the appearance of all instances of a class, use appearance to get the appearance proxy for the class.
In fact, that means that your changes will be applied to every instance of UILabel that enter your application window (as an iOS app has only 1 "public" window). If you want to customize the appearance of labels contained within a particular container class (e. g. inside UIView), you should consider using
+ appearanceForTraitCollection:whenContainedIn: (unfortunately, it's been deprecated since iOS 9.0). A much safer way is to use inheritance for your purposes as follows: MySpecialLabel -[inherits from and applies custom style attributes within its initializers and/or the awakeFromNib: method]-> UILabel.
B) Why is it related to UITextField?
That's because UITextField utilizes UILabels under the hood (like _placeholderLabel and _promptLabel).
I hope it will help you solve your problem.

Calling setAttributedText on UITextView resets the font size, and I can't figure out why

I've read about this annoying bug on Stackoverflow, where if you don't make your UITextView selectable, setting the text will cause the font to reset, which is super annoying.
This seems to only work for when setting the normal text, not attributedText, because when I set attributedText it still resets the font, even if I have my the text view set to selectable.
This is really easy to replicate. Create a text view in a storyboard for instance (how I'm doing it), set a font, then set the attributed text to a plain string with myTextView.attributedText = NSAttributedString(string: "Lorem ipsum dolor.") and it gets reset.
How do I stop this? It's super annoying.
I had the same issue and solved it with pragmatism as I'm sure it's a bug in iOS. So, my take might be not the best solution but one that'll work. Write a category for UITextView where with a method like -(void)setAttributedTextWithoutResettingFont:(NSAttributedString *)attributedText; and implement it such that your first save the current font in a temporary variable, then call the actual setter setAttributedText: on the UITextView and finally update the font again with the one saved in your temporary variable.

appearance proxy not working as intended for UIButton font

im currently styling my app via the appearance proxy and i ran into this problem:
when i set properties on the UIButton appearance my font is ignored:
[buttonAppearance setTitleColor:darkColor forState:UIControlStateNormal];
[buttonAppearance.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
the first line is applied properly (darkColor is some UIColor), but my font change is ignored completely.
When i copy the line into my ViewController and apply it to a concrete button it works fine.
Am i missing something?
any help appreciated! ty
The font name is wrong, it should be HelveticaNeue, without the space between.
In the future if you want to see other iOS font names you should check this website piece of code
EDIT
After a closer look I realized that you are trying to set the appearance of the button's title which is a UILabel, sadly UILabel doesn't have the font property in the UIAppearance proxy and that's why the font doesn't work.
I have found this class TWTButton.h, who resolved my problems adding a new appearance selector [setTitleFont:] to the UIButton class.
buttonAppearance = [TWTButton appearance];
[buttonAppearance setTitleFont:[UIFont systemFontOfSize:10.0f]];
You may read more about this here : http://toastmo.com/blog/2013/01/17/uiappearance/

UILabel how to tighten letter spacing

I would like to know how to programatically "Tighten Letter Spacing"?
This option is available for a UILabel made in a xib file, and is really handy sometimes.
I know this question has been asked before, but I don't see an answer that mentions this beeing available in the interface builder, so I was curious...
You are probably looking for this:
#property(nonatomic) BOOL adjustsLetterSpacingToFitWidth
Property of UILabel new in iOS6.
You might want to use UILabel property:
allowsDefaultTighteningForTruncation: Bool
and set it to true. (Default is false)
According to Apple Documentation:
When this property is set to true, the label tightens intercharacter spacing of its text before allowing any truncation to occur. The label determines the maximum amount of tightening automatically based on the font, current line width, line break mode, and other relevant information.
https://developer.apple.com/reference/uikit/uilabel/1620533-allowsdefaulttighteningfortrunca
adjustsLetterSpacingToFitWidth is deprecated as of iOS 7.
You'd now (as of iOS 8) probably want to do something like:
NSAttributedString *as =
[[NSAttributedString alloc] initWithString:#"Kerninating all the strings"
attributes:#{NSKernAttributeName : #(-2.0)}];
label.attributedText = as;

Resources