I'm using UIPickerView but in a willing to custom the font size in a row in a more efficient way.
First I have tried viewForRow: to create a label by myself, and call titleForRow: in it to acquire the text content that need to show. It works, but I'm not sure if it is a proper way.
Then I delete the method viewForRow: and tried attributedTitleForRow: that successfully changed the text color using NSForegroundColorAttributeName, but failed to set font size using NSFontAttributeName by next line. I doubt if there is a constraint mechanism that forbid/autoresize the font size change in UIPickerView? (The UIPickerView actually have a constraint of height combines to 162, 180 or 216 that I can only use CGAffineTransformMakeScale to custom it, so I really doubt it has another constraints.)
Now I'm very confused if there is a way modify the font size without implement viewForRow:, thanks.
Okay~~~ Now I'm implementing the viewForRow: method.
Related
I have a circumstance in my app whereby a label may be given a font size greater than it's height. This is to do with some rather complex architecture and layout code. Normally I would increase the label's height to accommodate the larger font but that is profoundly difficult to do in my scenario and I would rather avoid it.
The next logical thing is to turn clipsToBounds off, to allow the text sublayer to overflow the bounds of the label. Unfortunately this seems to have no effect in this case as the text is still clipped.
Am I missing something?
Looking at the documentation for UILabel:
https://developer.apple.com/documentation/uikit/uilabel/1620545-textrect
I think you need to override the method textRect(forBounds:limitedToNumberOfLines:) by explicitly increasing the rectangle returned by this method to the containing size of the label’s string rather than the bounds of the label.
(This solution does of course require you to subclass.)
Hope that helps.
You should be able to get the font height from font.lineHeight and then reduce the font size until the line height is less than the label height.
The reason (need citation) is that UILabel which is embeeded in UIButton cares extra glyph information embedded in font whereas an independent UILabel doesn't.
Solution
You can nest a separate UILabel on top of your UIButton and it will solve the problem. It's ugly but it works. There are few workarounds that you ought to try.
Workarounds
Depending on the scenario here is a small checklist that I found as accepted answer or useful for someone.
1) If you're using a UIButton Make sure you're using this method
[button setTitle forState:]
otherwise you'd need to use the following code to refresh the state
[myButton setNeedsLayout];
2) You might need to adjust your font size to fit the width of the label.
[yourLabel setAdjustsFontSizeToFitWidth:YES];
3) Although setting clipToBounds works in consecutive hierarchy, You might want not want to set individually on either Button or Label.
[yourButton setClipsToBounds:NO];
[yourButton.titleLabel setClipToBounds:NO];
There are few solutions that are pointing UIButton subclassing method which are essentially trying to add UIEdgeInset to button.
Am adding an image to a NSAttributedString object so I can display them properly in a UITextView.
So far, it works great. Only problem is, my Font size is 25 to match my image size but each time I add an image to the UITextView, the Font resets to some smaller size. I need to keep the font at 25 to match texts with the image size.
NSAttributedString has 3 constructors which take string, attributedString and string:attributes:.
Since I'm not using string, I can't set the attributes using the third constructor and the first 2 won't allow me to set attributes (UIFont). I therefore decided to set the Font size from Interface Builder and reset it each time I add an image to the `UITextField.
Am facing a problem here because each time I reset the font, the UITextView scrolls to the first line (assuming there is so much text, it has scrolled up). How do I set font size after adding image without the UITextView scrolling up automatically? Better still, is there a better way of doing this? Thanks.
NSMutableAttributed string has some methods you may try, including setting attributes across ranges after the attributed string is constructed.
I noticed in my iOS app, for text that are using a system font with size 20 pt or more, the bottom of text is being cut off.
For example, the bottom portion of the letters y, g, p, and g are being cut off.
How do I fix this in Xcode?
If your using something like a UILabel it means you will need to increase the vertical height of that element.. Either in code or in interface builder.
If you are doing it in code you can get the label to resize itself to fit the contents by calling sizeToFit..
[myLabel sizeToFit];
Alternatively you could measure the size to the string by using sizeWithAttributes: then change the size of the label accordingly.
Updating this one for Swift 3.x in a UITableViewController.
In cellForRowAtIndexPath after the problematic label is set, but before the cell is returned from the method, add a line something like this:
cell.myLabelWhoseCharactersLikegyqGetCutOff.sizeToFit()
I have a UITextField inside of a table cell and am trying to use interface builder to extend the width. However no matter how much I drag it, it remains at 97. Am I not allowed to extend it for some reason? Thanks
EDIT: Attempting to edit the text or increasing the font truncates the text..the UITextField will not change dimensions to accommodate it - it remains at 97 x 30.
Interface builder is tricky when it comes to some thing.
Your best bet is to modify the text field's frame in cellForRowAtIndexPath: in tableview's datasource method. That should fix this kind of problems.
We are using "TTTAttributedLabel" for displaying labels. For calculating the correct rectangle size, we use NSString's "sizeWithFont" method, with a "constrainedToSize" the width of the field.
The calculation is fine, unless there are some Emoji symbols in the text, and the text is multi line (for example: smiley-newLine-smiley). In that case, the returned size is too small (vertically), and the last line is not shown. If the text does not contain any Emoji (e.g. X-newLine-X) - the size is correct.
Our font is "HelveticaNeue" size:16.25, in case is makes any difference.
Is there a better way to calculate the needed size, so that it will work with Emoji as well?
Thanks
I had a same situation when making auto-height label according to the contents of the label.
Everything seems fine, except when there are emojis in label content.
It was because I did't use the correct setText method for AttributedString.
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^
return mutableAttributedString;
}];
This is the correct way to set AttributedString, but what I did is
[label setAttributedText:text];
So it was getting wrong label heights when it includes emojis in it.
I solved this problem by changing this set method with the correct one as described in Github manual.