core-text

Core Text - NSAttributedString line height done right?

↘锁芯ラ 提交于 2019-11-28 15:56:40
问题 I'm completely in the dark with Core Text's line spacing. I'm using NSAttributedString and I specify the following attributes on it: - kCTFontAttributeName - kCTParagraphStyleAttributeName From this the CTFrameSetter gets created and drawn to context. In the paragraph style attribute I'd like to specify the height of the lines. When I use kCTParagraphStyleSpecifierLineHeightMultiple each line receives padding at the top of the text, instead of the text being displayed in the middle of this

NSAttributedString add text alignment

不羁的心 提交于 2019-11-28 15:55:52
How can I add text alignment attribute to an NSAttributedString to center the text? Edit: Am I doing anything wrong? It doesn't seem to change the alignment. CTParagraphStyleSetting setting; setting.spec = kCTParagraphStyleSpecifierAlignment; setting.valueSize = kCTCenterTextAlignment; CTParagraphStyleSetting settings[1] = { {kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting}, }; CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting)); NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc] initWithAttributedString:self

Core Text calculate letter frame in iOS

白昼怎懂夜的黑 提交于 2019-11-28 15:16:16
I need to calculate exact bounding box for every each character (glyph) in NSAttributedString (Core Text). After putting together some code used to solve similar problems (Core Text selection, etc..), the result is quite good, but only few frames (red) are being calculated properly: Most of the frames are misplaces either horizontally or vertically (by tiny bit). What is the cause of that? How can I perfect this code?: -(void)recalculate{ // get characters from NSString NSUInteger len = [_attributedString.string length]; UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);

Get each line of text in a UILabel

↘锁芯ラ 提交于 2019-11-28 04:56:19
问题 I'm trying to add each line in a UILabel to an array, but the code I'm using doesn't appear to be terribly accurate. func getLinesArrayOfStringInLabel(label:UILabel) -> [String] { guard let text: NSString = label.text as? NSString else { return [] } let font:UIFont = label.font let rect:CGRect = label.frame let myFont: CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil) let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String) attStr

How does line spacing work in Core Text? (and why is it different from NSLayoutManager?)

和自甴很熟 提交于 2019-11-28 03:09:29
I'm trying to draw text using Core Text functions, with a line spacing that's as close as possible to what it would be if I used NSTextView. Take this font as an example: NSFont *font = [NSFont fontWithName:@"Times New Roman" size:96.0]; The line height of this font, if I would use it in an NSTextView is 111.0. NSLayoutManager *lm = [[NSLayoutManager alloc] init]; NSLog(@"%f", [lm defaultLineHeightForFont:font]); // this is 111.0 Now, if I do the same thing with Core Text, the result is 110.4 (assuming you can calculate the line height by adding the ascent, descent and leading). CTFontRef

Memory usage grows with CTFontCreateWithName and CTFramesetterRef

荒凉一梦 提交于 2019-11-27 22:40:24
问题 I'm writing an IOS program which uses custom fonts (CTFontManagerRegisterFontsForURL). I load the font, add it as a string attribute, create a framesetter, then a frame, and draw it to a context. I release everything i use. Instruments doesn't notice a leak but : The memory used by the applications grows and doesn't shrink when using this function. The retain count of my font is 2 when i leave the function. Here is the code : CFMutableAttributedStringRef attributedStringRef =

Vertical align with Core Text?

五迷三道 提交于 2019-11-27 22:26:22
How do I change the vertical alignment of the text in a CTFramesetter frame? I want my text to be in the middle instead of being at the top . I am using Core Text framework. There is a setting of the paragraph to change horizontal aligment but not vertical. Finally figured it out ... CGRect boundingBox = CTFontGetBoundingBox(font); //Get the position on the y axis float midHeight = self.frame.size.height / 2; midHeight -= boundingBox.size.height / 2; CGPathAddRect(path, NULL, CGRectMake(0, midHeight, self.frame.size.width, boundingBox.size.height)); Ken Thanks Nick, that was a great snippet.

How to split long NSString into pages

喜夏-厌秋 提交于 2019-11-27 20:15:11
I have a long NSString I want to display over a couple of pages. But to do this, I need to find out how much text will actually fit on the page. [NSString sizeWithFont: ...] Is not enough, it will just tell me if the text fits in the rectangle or not, if its does not, it will silently truncate the string, but it won't tell me where it truncated! I need to know the first word that does not fit on the page, so I can split the string and draw that part of it on the next page. (and repeat) Any ideas how to solve this? Only idea I have myself so far is to repeatedly call sizeWithFont

CTFramesetterSuggestFrameSizeWithConstraints sometimes returns incorrect size?

邮差的信 提交于 2019-11-27 17:08:31
In the code below, CTFramesetterSuggestFrameSizeWithConstraints sometimes returns a CGSize with a height that is not big enough to contain all the text that is being passed into it. I did look at this answer . But in my case the width of the text box needs to be constant. Is there any other/better way to figure out the correct height for my attributed string? Thanks! CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString); CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.view.bounds.size.width,

Draw underlined / strikethrough text ( MULTILINE STRING )?

久未见 提交于 2019-11-27 16:52:30
问题 I have to draw underlined-multiline text with all types of text alignment. I have searched on forums and got some results like: http://davidjhinson.wordpress.com/2009/11/26/underline-text-on-the-iphone/ http://forums.macrumors.com/showthread.php?t=561572 But all draw text for single line only. while i have multi-line text. The situation even become worse when the text alignment is centered. I searched and found that in iphone-sdk-3.2 there are some core-text attributes for underlining a text