我正在使用CoreText来呈现多列文本.但是,当我将第一段的第一个字母设置为比文本的其余部分更粗体,更大的字体时,我会产生2个问题(在附加图像中都可见):
第一行下面的间距太大(我明白这是因为第一个字符可能是ag,y,p,q等.
第一行下面的行现在不与下一列中的相应行对齐.
如何克服这两个问题的任何建议将不胜感激,谢谢.

我想要一些带有自定义行距的文本,所以我写了一个属性字符串CTParagraphStyleAttributte并将其传递给我的CATextLayer:
UIFont *font = [UIFont systemFontOfSize:20];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize, NULL);
CGColorRef cgColor = [UIColor whiteColor].CGColor;
CGFloat leading = 25.0;
CTTextAlignment alignment = kCTRightTextAlignment; // just for test purposes
const CTParagraphStyleSetting styleSettings[] = {
{kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &leading},
{kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(styleSettings, 2));
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)ctFont, (id)kCTFontAttributeName,
(id)cgColor, (id)kCTForegroundColorAttributeName,
(id)paragraphStyle, (id)kCTParagraphStyleAttributeName,
nil];
CFRelease(ctFont);
CFRelease(paragraphStyle);
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
initWithString:string
attributes:attributes];
_textLayer.string = attrStr;
[attrStr release];
Run Code Online (Sandbox Code Playgroud)
但是行高没有改变。我想我在这里遗漏了一些东西,但我不知道是什么。
我已经尝试过kCTParagraphStyleSpecifierLineSpacingAdjustment …
这是我用来在核心图形中显示一些文本的代码:
int y = MESSAGE_FRAME.origin.y + 8;
if (month) y = y + 27;
int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];
CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);
UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];
[[UIColor colorWithWhite:0.2 alpha:1.0] setFill];
[self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
Run Code Online (Sandbox Code Playgroud)
如果我想在核心文本中显示相同的文本,这是代码:
CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height + 2, MESSAGE_FRAME.size.width - 16, height);
UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];
CGFloat lineHeight = [font lineHeight];
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL); …Run Code Online (Sandbox Code Playgroud) 我有类似这个问题的问题,但更复杂.我正在尝试在表格视图单元格中绘制文本,其中右上角是日期标签,右下角是徽章,如下所示:
+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur +----+
| adipiscing elit. Integer |
| ligula lectus, convallis |
| non scelerisque +-----+
| quis, tempor at nibh |BADGE|
+----------------------+-----+
Run Code Online (Sandbox Code Playgroud)
如果有更多或更少的文本,我希望单元格更大或更小:
+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur +----+
| adipiscing elit. Integer |
| ligula lectus, convallis |
| non scelerisque quis, |
| tempor at nibh. Curabitur |
| eget diam ligula. |
| Pellentesque a elit |
| dolor, …Run Code Online (Sandbox Code Playgroud) 请帮我理解以下代码的问题:
NSString *fontName = @"ArialMT";
CGFloat fontSize = 20.0;
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
NSString *characters = @"ABC";
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
NSLog(@"*** CTFontGetGlyphsForCharacters failed.");
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
我正在试图确定,哪个特定字符UITextView已被挖掘.我试着用characterRangeAtPoint:方法.但它总是会返回零,无论UITextView我在哪里点击.我甚至编写并运行以下代码:
for (int x = 0; x<1000; x++) {
pos.x = x;
for (int y = 0; y<1000; y++) {
pos.y = y;
UITextRange * range = [textView characterRangeAtPoint:pos];
if (range!=nil) {
NSLog(@"x: %f, y: %f", pos.x, pos.y);
}
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,它永远不会到达NSLog字符串.我究竟做错了什么?
我已经将泄漏问题减少到这个易于编译的代码中,该代码在CTFontCreateWithGraphicsFont使用和发布后显示ct_font,cg_font将留下一个额外的引用。这是 Apple 内部引用计数问题还是我错过了一些东西,例如必须双重发布cg_font或更改发布顺序?谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <ApplicationServices/ApplicationServices.h>
int main(int argc, char **argv) {
FILE *f = fopen("/Library/Fonts/Tahoma.ttf", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char* font = (char*)malloc(fsize);
fread(font, fsize, 1, f);
fclose(f);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, font, fsize, NULL);
CGFontRef cg_font = CGFontCreateWithDataProvider(provider);
CTFontRef ct_font = CTFontCreateWithGraphicsFont(cg_font, 36., NULL, NULL);
CGDataProviderRelease(provider);
//
CFRelease(ct_font);
CFRelease(cg_font);
printf("ct_font: %d\ncg_font: %d\n", (int)CFGetRetainCount(ct_font), (int)CFGetRetainCount(cg_font));
free(font);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译运行后的结果:
ct_font: …
我正在尝试将 aUILabel中的每一行添加到数组中,但我使用的代码似乎并不十分准确。
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.addAttribute(NSAttributedStringKey.font, value:myFont, range: NSMakeRange(0, attStr.length))
let frameSetter:CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
let path: CGMutablePath = CGMutablePath()
path.addRect(CGRect(x: 0, y: 0, width: rect.size.width, height: 100000))
let frame:CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
let lines …Run Code Online (Sandbox Code Playgroud) 我得到了 CGGlyph
let a = CTFontGetGlyphWithName(font, "A" as CFString)
let v = CTFontGetGlyphWithName(font, "V" as CFString)
Run Code Online (Sandbox Code Playgroud)
我得到了一张桌子的链接
let kernData = CTFontCopyTable(font, CTFontTableTag(kCTFontTableKern), CTFontTableOptions(rawValue: 0))
Run Code Online (Sandbox Code Playgroud)
字距调整表
let kerningTable = CFDataGetBytePtr(kernData)
Run Code Online (Sandbox Code Playgroud) 我在addAttributes无法重现的栏位(EXC_BREAKPOINT)上经常出车祸。在我传递的NSRange上添加了检查,令人惊讶的是它仍然崩溃...非常感谢收到任何想法!
let boldAttributes : [NSAttributedStringKey: Any] = [.font: cellStyle.boldFont()]
if (nsrange.location >= 0 && nsrange.length > 0 && nsrange.location + nsrange.length <= myAttributedString.length) {
myAttributedString.addAttributes(boldAttributes, range: nsrange)
}
Run Code Online (Sandbox Code Playgroud)
通过请求添加的崩溃日志:
#0. Crashed: com.apple.main-thread
0 MyApp 0x10054ae38 specialized UIAccessorizedTextField.accessoryAttributedString(IndexPath, cellStyle : UIAccessorizedTextFieldCellStyle) -> NSAttributedString (UIAccessorizedTextField.swift:322)
1 MyApp 0x10054b104 specialized UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift:345)
2 MyApp 0x10054860c @objc UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift)
3 UIKit 0x18b229f3c -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 356
4 UIKit 0x18bb90b68 -[UICollectionView _prefetchItemsForVelocity:maxItemsToPrefetch:invalidateCandidatesOnDirectionChanges:] + …Run Code Online (Sandbox Code Playgroud) core-text ×10
ios ×8
objective-c ×4
iphone ×3
swift ×3
macos ×2
c ×1
catextlayer ×1
cocoa ×1
cocoa-touch ×1
ipad ×1
refcounting ×1
uikit ×1
uitableview ×1