Core Text

RSS for tag

Create text layouts, optimize font handling, and access font metrics and glyph data using Core Text.

Core Text Documentation

Posts under Core Text tag

27 Posts
Sort by:
Post marked as solved
1 Replies
887 Views
Although Text will render two lines if you pass it a 2 line String, it will render the text of both lines concatenated if you pass it as a markdown string. Same if you create an AttributeText before. Examining the AT highlights the markdown structure (NSPresentationIntent, NSInlinePresentationIntent, NSLink, Header, Paragraph, CodeBlock, etc) but this is "flattened" if you will (NSLink works, multiline CodeBlock work). The problem isn't only present in Text. My use case was CoreText 2: I expected that each AttributeText run would be turned into a text layout fragment but NSTextLayoutManager also flattens my example. As an example, a screenshot of a markdown file in Xcode and the rendering in the simulator. 2 "paragraphs" aka 2 text layout fragments. The same test with the EGG recipes rtf file of the CoreText2 WWDC21 session gives predictable results, one text layout fragment per paragraph. I'm wondering if this is a bug or a "feature" of this markdown first iteration? I tried to access the NSPresentationIntent without much luck to populate CoreText2 myself. Any hint would be REALLY welcomed. thank.
Posted
by seri.
Last updated
.
Post not yet marked as solved
0 Replies
375 Views
Is there a way to import custom font into Reality composer on iPad? I can see them properly on desktop but I'm trying to figure out how to do the same on Ipad so that when I export my project on the remote device there is visual parity. Thanks
Posted
by Dan1979.
Last updated
.
Post marked as solved
1 Replies
437 Views
I'm investigating some font/typography issues in a creative application. I am hoping for some information from Apple developers who know how font activation works in Core Text, and how/whether auto-activation is still supported. I'm running on Big Sur in an Obj-C Cocoa app. In applicationDidFinishLaunching the app calls CTFontManagerSetAutoActivationSetting(NULL, kCTFontManagerAutoActivationEnabled); After this, what is the correct API to request auto-activation of a font that may not be active but could be activated, e.g. by a third-party font manager? I have tried using CTFontCreateWithNameAndOptions. For options, I see there is an CTFontOption value kCTFontOptionsPreventAutoActivation - I assume that as long as I don't set this option, the call will attempt auto-activation. However, I don't see any auto-activation resulting, either from the third-party font manager or the OS. Instead CTFontCreateWithNameAndOptions always returns a substitute font (Helvetica). The font I am attempting to auto-activate is an OTF. It is stored in a third-party font manager, and I also have copies of it on the desktop. I have also tried having it present / disabled in FontBook, which also does not auto-activate it in this case. I've also cleaned font caches after making changes. How is the kCTFontManagerAutoActivationEnabled supposed to work in Big Sur? Is there a different Core Text call used to auto-activate a font, or is this behavior no longer supported? Thanks!
Posted
by ccorbell.
Last updated
.
Post not yet marked as solved
0 Replies
553 Views
I am using GeometryReader to calculate the size of font to use to fit several lines of text, however it is always too big. What can be wrong with my calculation? Here's a simply Playground import SwiftUI import PlaygroundSupport struct MainView : View { var body: some View { VStack { SomeView() .frame(width: 508.0, height: 246.5) } } } struct SomeView: View { let newData = ["1", "2", "3", "4", "5", "6", "7", "8"] var lines: Int { newData.count } var body: some View { GeometryReader { geometry in VStack(alignment: .leading, spacing: 0) { ForEach(0..<lines) { idx in Text(newData[idx]) .padding(0) } } .font(.system(size: geometry.size.height / CGFloat(lines))) .onAppear { print(geometry.size) print(lines) print(geometry.size.height / CGFloat(lines)) } } } } PlaygroundPage.current.setLiveView(MainView()) This gives me this result which I need to show all 8 lines:  The GeometryReader correctly reports the height of the box to be 246.5 and the number of lines to be 8 which results in a 30.8125 font size. It seems the font size needs some additional padding but how do I calculate this? I want to be able to supply an unknown amount of data and have it fill the box. (Well I am actually using a LazyVGrid which is suffering the same issue).
Posted
by dazboj.
Last updated
.
Post not yet marked as solved
1 Replies
625 Views
What is the currently recommended way to create a pdf document in Xcode 13 for Mac OSX using Swift and SwiftUI? I find very little documentation on this. Most articles are for IOS. Only some articles for OSX from many, many years back. With SwiftUI, it will become common place for our code to be used interchangeable in multiple operating systems. And pdf is pretty much the way to go with document and report printouts. Please point me in the right direction. Maybe I was just not looking in the right place. My pdf documents are created without any problem for IOS and Ipad using CG and PDFKit functions. But the same code did not work for the Mac app. So I modified the UIFont to NSFont, etc. Context functions are different. Moved to using CoreText for the attributed string placements. ( CTFrame ) But the strange stuff is the Y point placement that is correct for IOS but seems to be reversed for OSX. So this was when I thought I must be using some old stuff that is not meant for today's work. Brief Example below: func pdf_placeText_CT( cgContext: CGContext, pageSize: CGSize, text: String, width: CGFloat, height: CGFloat, font: NSFont, alignment: String, xPoint: CGFloat, yPoint: CGFloat ) {         let pointY = pageSize.height - yPoint  // for some reason we have to flip the vertical. Strange.   let paragraphstyle = NSMutableParagraphStyle()           if ( alignment == "R" ) {       paragraphstyle.alignment = .right     } else if ( alignment == "C" ) {       paragraphstyle.alignment = .center     } else {       paragraphstyle.alignment = .left     }         let attributedText = NSAttributedString(string: text, attributes: [       NSAttributedString.Key.font      : font ,       NSAttributedString.Key.paragraphStyle : paragraphstyle      ])         let stringSize = attributedText.size()   let stringRect = CGRect(x: xPoint, y: pointY, width: stringSize.width+5, height: stringSize.height+5)   let stringPath = CGPath(rect: stringRect, transform: nil)    //attributedText.draw(in: stringRect)       let textFrame = CTFramesetterCreateWithAttributedString(attributedText)   let textRange = CFRangeMake(0, attributedText.length)   var pageRange = CFRange()   CTFramesetterSuggestFrameSizeWithConstraints(textFrame, textRange, nil, pageSize, &pageRange)       let frame = CTFramesetterCreateFrame(textFrame, pageRange, stringPath, nil)       CTFrameDraw(frame, cgContext )      }
Posted
by bbrittain.
Last updated
.
Post not yet marked as solved
2 Replies
5.9k Views
I have a String extension function which converts an html string to a NSAttributed string, and it's made up with Swift 3.x. The strings I'm showing in a UILabel consists of different html-tags, like &lt;i&gt;, &lt;b&gt;, and &lt;center&gt;.Using Xcode 9.0.1 and deploying the app with the code onto an iPhone running iOS 10 it's working perfectly.Same app, but deployed to an iPhone running iOS 11, all html-tags are converted - except the &lt;b&gt;.Here's the code: func htmlAttributedString(fontSize: CGFloat = 17.0) -&gt; NSAttributedString? { let fontName = UIFont.systemFont(ofSize: fontSize).fontName let string = self.appending(String(format: "&lt;style&gt;body{font-family: '%@'; font-size:%fpx;}&lt;/style&gt;", fontName, fontSize)) guard let data = string.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil } guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else { return nil } print("\(String(describing: self)):\(#function): attributedString: \(attributedString)") return attributedString }By looking at the attributedString output to the console, I can see that on the iPhone with iOS 10 the attributed string contains .SFUIText, .SFUIText-Italic, and .SFUIText-BoldItalic as it should, but running iOS 11 the .SFUIText-BoldItalic is missing.Have any one else experienced the same issue, and maybe has come up with a solution?//Mellberg
Posted
by Mellberg.
Last updated
.
Post not yet marked as solved
0 Replies
370 Views
iPhones have this innate feature where you can long-press on a word and select look-up to see its definition. Is there a way to store what word is currently being looked-up/which words have been looked-up in the past, using some kind of query/framework?
Posted Last updated
.