Core Text

RSS for tag

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

Posts under Core Text tag

23 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

iOS 17:CTFramesetterSuggestFrameSizeWithConstraints does not provide the correct text size.
In iOS 17, when I try to use CTFramesetterSuggestFrameSizeWithConstraints to obtain the size of a text, if the system-provided bold font is used, the resulting rectangular area may not be sufficient to accommodate the text. The provided example code is not allowing me to display the complete text within the CTFrame. code: NSString *testString = @"《测试》"; CFStringRef cfTestString = CFStringCreateWithCString(kCFAllocatorDefault, [testString cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8); UIFont *uiFont = [UIFont boldSystemFontOfSize:50.0]; UIFontDescriptor *fontDescriptor = uiFont.fontDescriptor; CTFontDescriptorRef ctFontDescriptor = (__bridge CTFontDescriptorRef)fontDescriptor; CTFontRef ct_font = CTFontCreateWithFontDescriptor(ctFontDescriptor, uiFont.pointSize, NULL); CGFloat color_components[4] = {1.0, 1.0, 1.0, 1.0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorRef cgColor = CGColorCreate(colorSpace, color_components); CTParagraphStyleSetting styleSetting; styleSetting.spec = kCTParagraphStyleSpecifierAlignment; CTTextAlignment ctAlignment = kCTTextAlignmentCenter; styleSetting.value = &ctAlignment; styleSetting.valueSize = sizeof(kCTTextAlignmentCenter); CTParagraphStyleRef ctParagraphStyle = CTParagraphStyleCreate(&styleSetting, 1); const void* keys[] = {kCTFontAttributeName, kCTForegroundColorAttributeName, kCTParagraphStyleAttributeName}; const void* values[] = {ct_font, cgColor, ctParagraphStyle}; CFDictionaryRef attr = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFAttributedStringRef cfAttributedString = CFAttributedStringCreate(kCFAllocatorDefault, cfTestString, attr); CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(cfAttributedString); CGSize constraints = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX); CGSize stringSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, constraints, NULL); float width = ceil(stringSize.width); float height = ceil(stringSize.height); CGPathRef path = CGPathCreateWithRect(CGRectMake(0.0, 0.0, width, height), NULL); CTFrameRef cfFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); CFRange stringRange = CTFrameGetStringRange(cfFrame); CFRange visibleStringRange = CTFrameGetVisibleStringRange(cfFrame); NSLog(@"stringRange.location: %@ stringRange.length: %@", @(stringRange.location), @(stringRange.length)); NSLog(@"visibleStringRange.location: %@ visibleStringRange.length: %@", @(visibleStringRange.location), @(visibleStringRange.length)); log: stringRange.location: 0 stringRange.length: 4 visibleStringRange.location: 0 visibleStringRange.length: 2
2
0
811
Oct ’23
Cursor Disappearing with Multiple NSTextViews
I have a Mac app, Notenik, written with AppKit, that dynamically generates an Edit view that is used to edit multiple fields for a Note. Sometimes, depending on a user's request, multiple NSTextViews end up appearing within the same parent view (a tab, if that matters). Lately I and some of my users have noticed that, when moving from one NSTextView to another (either by tabbing or by clicking), the cursor initially fails to appear. Once the user starts editing within the NSTextView, the cursor appears and acts normally. And this doesn't occur when moving from an ordinary text field to an NSTextView -- only when moving from one NSTextView to another. But it is troubling and confusing to users that the cursor initially disappears. I'm not sure exactly when this started happening, but it has not been happening for very long. Although I make frequent updates to the app, this section of code has had no recent changes, so I am at a loss to explain this behavior, or to correct it. Any help would be appreciated.
1
0
855
Aug ’23
Text Kit not word-wrapping first/last lines in UITextView
When using an exclusion path with an UITextView, the text view is not properly wrapping the words for the first and last line in the example shown (lower text). It does render properly using word-wrap with Core Text (top example).(Hmm - the image link I provided in the HTML here works with the preview, but not the post. The example image is at: http i.stack.imgur.com/Z91ge.pngHere is the code for the UITextView (both this and the Core Text example use the same size bezier path, and both use the appropriate corresponding font and paragraph settings; wrap by word and centered):NSString *testText = @"Text Kit exclusion paths ..."; / UIBezierPath *viewPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 280, 280)]; UIBezierPath *shapePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 265, 265)]; viewPath.usesEvenOddFillRule = true; shapePath.usesEvenOddFillRule = true; [shapePath appendPath:viewPath]; NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:testText]; UIFont *font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:14]; [title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)]; [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setAlignment:NSTextAlignmentCenter]; [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping]; [title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)]; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 370.0, 280, 280)]; textView.textContainerInset = UIEdgeInsetsMake(0,0,0,0); textView.textContainer.exclusionPaths = @[shapePath]; [textView.textContainer setLineBreakMode:NSLineBreakByWordWrapping]; textView.contentInset = UIEdgeInsetsMake(0,0,0,0); textView.attributedText = title; textView.backgroundColor = [UIColor clearColor];It is worth noting that the Text Kit is respecting the word wrapping rules except for the first and (possibly last) line where the text does not actually fit. I need this to work with a UITextView because text entry is required, or I would be done with my Core Text example.I have tried everything I can think of to reliably work-around the issue. I don't fully understand how UITextView is working with Core Text (or I would expect to get the same results which I don’t), so any suggestions would be greatly appreciated.
3
0
2.0k
Oct ’23