How to autoresize text with NSAttributedString - ios

I have a button which opens a dropdown menu.
so, inside the button i have a text and a font awesome icon (down arrow)
i used attributed string to set the text and the icon separately. (had problems with the text being truncated)
now it looks fine, but i need to provide the font size as part of the attributes.
i want the font to auto resize - if the screen is larger and the button gets bigger - so should the text.
this is the attribute string part
let iconPart = NSMutableAttributedString(string: "\u{F107}", attributes: [NSAttributedStringKey.font: UIFont(name: "fontello", size: 23)!])
let textPart = NSMutableAttributedString(string: "Category ", attributes: [NSAttributedStringKey.font: UIFont(name: "Gotham", size: 19)!])
i've seen this solution:
let systemDynamicFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .subheadline)
let size = systemDynamicFontDescriptor.pointSize
let font = UIFont(name: "Gotham", size: size)
but this seems to keep the font size fixed according to the text style (.subheadline in this example)
is there a way of doing it?

Related

How to apply Atrributed String different spacing between lines in swift

I want to apply spacing between first two lines in attributed string and third line should look like paragraph.
Expected output :
Expected output screenshot
Current Implemenation:
Current implementaion screenshot
Here is the code tried by me.
let myString = "Your account phone numbers are listed here.\nTo change or delete a phone number, tap on it.\nTo add a phone number, go to the top right-hand corner of your screen and tap on “Add”.";
let font = UIFont.systemFont(ofSize: 14)
let attributedString = NSMutableAttributedString(string: myString, attributes: [.font: font])
self.displayLabel.attributedText = attributedString
I created label and setting number of lines 0 so it will display multiline text.
In the label need to show space in the first two lines as shown in expected output screenshot.
How to apply spacing only to first two lines and third line should display as shown in expected output screenshot?
You seem to want to set the spacing between paragraphs. This is controlled by NSParagraphStyle.paragraphSpacing. Just set the .paragraphStyle attribute of the attributed string to an NSParagraphStyle:
let paraStyle = NSMutableParagraphStyle()
paraStyle.paragraphSpacing = 10 // or some other number
let attributedString = NSMutableAttributedString(string: myString,
attributes: [
.font: font,
.paragraphStyle: paraStyle
])

NSAttributedString text always sticks to bottom with big lineHeight

I'm trying to implement by-design labels coming from Sketch e.g. I need text styles with font size = 19 and line height = 50. So I ended up using NSAttributedString with NSMutableParagraphStyle but was stopped by problem with text being sticked to bottom of UILabel
I've already tried to use lineHeightMultiple and lineSpacing but those didn't give me the line height I wanted so I ended up using minimumLineHeight and maximumLineHeight equal the same
Here is my approach to make NSAttributedString
private static func makeAttributedString(
with attributes: TextAttributes,
text: String? = nil,
alignment: NSTextAlignment = .center
) -> NSAttributedString {
let font = UIFont(name: attributes.font.rawValue, size: attributes.fontSize)!
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.paragraphSpacing = attributes.paragraph
paragraph.minimumLineHeight = attributes.lineHeight // equal 50 in my case
paragraph.maximumLineHeight = attributes.lineHeight // equal 50 in my case
let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.paragraphStyle: paragraph,
NSAttributedStringKey.foregroundColor: attributes.textColor,
NSAttributedStringKey.kern: attributes.kern,
NSAttributedStringKey.font: font
]
return NSAttributedString(string: text ?? "", attributes: attributes)
}
I expect result similar to design
but actually getting
Note: setting height constraint to 50 is not applicable because I also need multiline labels but there is the same bug with them
Seems like I've found some workaround myself, maybe it will help someone.
The method is about setting baselineOffset like this:
NSAttributedStringKey.baselineOffset: (attributes.lineHeight - font.lineHeight) / 4
Works like charm:
https://i.imgur.com/a2EOf5R.png

how to change the default font and font size of NSMutableAttributedString

I'm using swift. Is there any way to change default font/ font size etc of an NSMutableAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string.
You can use like that
let yourAttributes = [NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline), NSForegroundColorAttributeName: UIColor.blackColor()]
let yourString = NSAttributedString(string: "Here my string !", attributes: yourAttributes)
Thanks

Weird swift behaviour on UITextField

Scenario:
I'm setting the text font and size for my UITextField.
They also have placeholders.
The code bellow is in my UIViewController:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 18)
let attributesDictionary = [NSForegroundColorAttributeName: DefaultSystemColor.DarkRed]
// Textfileds without borders (and placeholders)
UsernameTextField.borderStyle = UITextBorderStyle.None
UsernameTextField.font = font
UsernameTextField.textColor = DefaultSystemColor.DarkRed
UsernameTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: attributesDictionary)
I'm configuring (in AppDelegate) a global UI setting, that formats all of my UILabels for a certain font size.
The code bellow is in my GlobalUISettings class:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 13)!
var labelAppearace = UILabel.appearance()
labelAppearace.font = font
What's weird in here:
When this UITextField is selected and I'm typing the format is the one I set for the text field (the placeholder are OK too).
But when I leave the field it assumes the behaviour of the UILabel.
I know that because if I comment the UILabel format the textField works properly.
Does anybody have any idea why does it happens?
Try changing the let font to let labelFont because 'font' is global. It might be affecting.
This is correct behavior.
To change the font of the placeholder you have to add the NSFontAttributeName attribute with the desired font as value to the NSAttributedString you assign to attributedPlaceholder.
Example:
let attributesDictionary = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: DefaultSystemColor.DarkRed
]

Font attribute (NSFontAttributeName) not applied to attributed string

I'm trying to apply two attributes to a string, one for kerning (NSKernAttributeName) and one for the font (NSFontAttributeName). Although I've checked and rechecked 1000 times, I can only get the kerning attribute to be applied to the string. Here's my setup:
let runAtTitle = "RUN AT"
var attRunAt = NSMutableAttributedString(string: runAtTitle)
let font = UIFont(name: "HelveticaNeue-Bold", size: 76.0)!
let attrs = [NSFontAttributeName: font, NSKernAttributeName: -3.0]
attRunAt.addAttributes(attrs, range: NSMakeRange(0, attRunAt.length))
runAtLabel.attributedText = attRunAt
When I build the app, the kerning is applied to my string, but the font is not. It uses the default 12pt Helvetica. Please tell me I'm doing something wrong.
I just tried your code in Playground and it works fine. Most likely you are setting the text attribute of the label after setting the attributedText. That will revert the string to the normal label string with its attributes, or perhaps for some strange reason keep the kern attribute.
Here is what I get with your code:
and after adding
label.text = "RUN AT"

Resources