NSTextField How to Set NSAttributedString's TextStyle property - swift

I am trying to programmatically create a NSTextField that has the NSFont.TextStyle.headline font style. This font can also be set from the font drop down menu in the storyboard.
However, when I do the following:
let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : NSFont.TextStyle.headline]
let attributedString = NSAttributedString(string: product.url, attributes: attributes)
let textLabel = NSTextField(labelWithAttributedString: attributedString)
My app crashes with the following error:
-[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff8064f918

We need to get the corresponding font associated with the text style.
Pass these constants to preferredFont(forTextStyle:options:) or preferredFontDescriptor(forTextStyle:options:) to retrieve the corresponding font or font descriptor.
Therefore:
let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : NSFont.preferredFont(forTextStyle: NSFont.TextStyle.headline, options: [:])]

Related

How can set accessibility identifier in attributedPlaceholder

I have the given piece of code
textField.attributedPlaceholder = NSAttributedString(string: Strings.test,
attributes: [NSAttributedString.Key.foregroundColor: ColorTheme.test])
How I can set an accessibility identifier in attributedPlaceholder so that I can later use the containing string in the UITest?
You need to give accessibility identifier to your textfield. Then you can get the placeholder text via property.
let textField = XCUIApplication().textFields["textFieldIdentifier"]
let placeholderText = textField.placeholderValue

CFAttributedString changing font size has no effect

I'm trying to draw text using CoreText and CoreGraphics. The text draws and positions fine, however I can't seem to be able to change font size. Or font for that matter. I tried setting attributed text with CFAttributedString directly and I tried changing the CGContext font size setting. Here's my code
//... (create CGContext)
let textAttrs = [
kCTFontSizeAttribute as String : 18 as NSNumber
] as CFDictionary
let attrString = CFAttributedStringCreate(kCFAllocatorDefault,
NSString(string: "Hello, world!"),
textAttrs)
let textLine = CTLineCreateWithAttributedString(attrString!)
context.textPosition = CGPoint.zero
CTLineDraw(textLine, context.textPosition)
The problem is with the definition of attributes. First I had to define font and then apply font as attribute for string.
let fontAttributes = [
kCTFontFamilyNameAttribute : "Courier",
kCTFontStyleNameAttribute : "Bold",
kCTFontSizeAttribute : 18.0
] as NSDictionary
let descriptor = CTFontDescriptorCreateWithAttributes(fontAttributes)
let font = CTFontCreateWithFontDescriptor(descriptor, 0.0, nil)
let attributes = [kCTFontAttributeName : font] as CFDictionary
let attrString = CFAttributedStringCreate(kCFAllocatorDefault,
"Hello, world!" as NSString,
attributes)

Multiline multicolor attributed text in swift

How can I build something like this using attributed text? I tried adding line feed (\n), it didn't work, just displayed first line and cropped next two, removing \n will display two parts in single line:
let mutableAttributedString = NSMutableAttributedString()
let regularAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 45.0), NSAttributedStringKey.foregroundColor: UIColor.yellow]
let boldAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 25.0), NSAttributedStringKey.foregroundColor: UIColor.white]
let mutableAttributedString2 = NSMutableAttributedString()
let boldAttributedString = NSAttributedString(string: "person", attributes: boldAttribute)
let regularAttributedString = NSAttributedString(string: "\(self.requestCount)", attributes: regularAttribute)
mutableAttributedString2.append(boldAttributedString)
mutableAttributedString2.append(NSAttributedString(string: "\n"))
mutableAttributedString2.append(regularAttributedString)
self.btnStatsPeople.setAttributedTitle(mutableAttributedString2, for: .normal)
UILabel has one line by default.
This property controls the maximum number of lines to use in order to
fit the label’s text into its bounding rectangle. The default value
for this property is 1. To remove any maximum limit, and use as many
lines as needed, set the value of this property to 0.
UIButton creates default UILabel. So using 0 lines is a solution for your problem:
self.btnStatsPeople.titleLabel?.numberOfLines = 0

Possible error in NSMutableAttributedString

I am trying to change the font of an label with should be displayed in the UI. I've used this lines:
let myAttribute = [ NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)! ]
let buttonTitleNSMut = NSMutableAttributedString(string: "\(buttonTitle)", attributes: myAttribute )
let buttonTitle:String = "\(buttonTitleNSMut)"
let button = createButtonWithTitle(buttonTitle as String)
But my output isn't the label in the different font, but a long string with all the "specs" of the font...
What am I doing wrong? Maybe the fact that in the end I say it is a String?
Try the code snippet below for what you are trying to achieve.
let plainText = "\(buttonTitle)" as NSString
let attributedText = NSMutableAttributedString(string: plainText as String)
attributedText.addAttribute(NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)!)
button?.titleLabel?.attributedText = attributedText
Hope that helps.

how to make text bold for detailedTextLabel in swift

cell!.textLabel?.text = vehicle["vrn"].string
cell?.detailTextLabel?.text = stateString
I want to display stateString as bold and also tried to use textLabel instead of detailedText but it did not work.
You can set the font property of the detailTextLabellike so:
cell.detailTextLabel?.font = UIFont.boldSystemFontOfSize(15.0)
You can use the font property inside the UILabel class.
// You need to set the name of your font here
cell.detailTextLabel?.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
There are other options using the attributedText
property but implies a little more of code, something like this:
// Define attributes to set
let labelFont = UIFont(name: "HelveticaNeue-Bold", size: 16)
let attributes :Dictionary = [NSFontAttributeName : labelFont]
// Create the attributed string
var attrString = NSAttributedString(string: "textOfYourLabel", attributes:attributes)
cell.detailTextLabel?.attributedText = attrString
I hope this help you.
If you want to bold text label using story board, simply select SHADOW colour in attributes inspector to same of that Text colour. Its works for me try it....