UILabel我想知道在表视图单元格中使用实例与 CATextLayers 的性能差异。虽然UILabel由于界面生成器的支持而非常方便,并且具有一些附加功能,例如提供阴影的方式,CATextLayer可以访问更多字体,并且应该是更靠近图形硬件的一层(原文如此!)。顺便说一句,有谁知道UILabel内部是否可能使用CATextLayers ?
为了将便利性和对更多字体的访问结合起来,可以从 派生UILabel,将图层类更改为CATextLayer并根据标签属性配置图层属性。如果标签drawRect()是空的,这不应该有太多的惩罚(除了额外的UIView惩罚),不是吗?
我有一个使用 HTML 制作的属性字符串DTCoreText,我想将其设置为 UILabel 的值。但是,我想稍后检查与 UILabel 相比,字符串的值是否已更改。是否可以同时设置 UILabeltext和attributedText属性?是否会attributedText简单地掩盖text属性,以便text属性可以保留为内部值?
我不确定这个函数的作用或应该做什么。
\nvoid CGContextSetTextMatrix (\n CGContextRef c,\n CGAffineTransform t\n);\nRun Code Online (Sandbox Code Playgroud)\n参考资料并不是那么有帮助。:(
\n\n\n设置当前文本矩阵。
\n文本矩阵指定从文本空间到用户空间的变换。为了生成用于在页面上实际绘制文本的最终文本渲染矩阵,Quartz 将文本矩阵与当前变换矩阵和图形状态中的其他参数连接起来。
\n注意,文本矩阵不是图形状态的一部分\xe2\x80\x94保存或恢复图形状态对文本矩阵没有影响。文本矩阵是图形上下文的属性,而不是当前字体的属性。”
\n
你能给我解释一下吗?
\n我通过以下方式动态注册字体:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([url isFileURL])
{
// Handle file being passed in
NSLog(@"handleOpenURL: %@",url.absoluteString);
NSData *inData = [NSData dataWithContentsOfURL:url];
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
UIFont *font;
if (!CTFontManagerRegisterGraphicsFont(fontRef, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", error);
CFRelease(errorDescription);
} else {
CFStringRef fontNameRef = CGFontCopyPostScriptName(fontRef);
NSLog(@"fontNameRef: %@",fontNameRef);
font = [UIFont fontWithName:(__bridge NSString *)fontNameRef size:80];
[self.arrayOfFonts addObject:(__bridge NSString *)fontNameRef];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFont" object:nil];
CFRelease(fontNameRef);
}
CFRelease(fontRef); …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一些行为与字体管理器非常相似的东西,但我根本找不到任何关于如何将 OpenType 字体添加到 NSFontManager 提供的列表中的文档。我什至看到 Font Book 显示通过字体管理器应用程序激活的字体,所以我想了解引擎盖下发生了什么。正在调用哪些库?
谢谢!
我试图按照关于 CoreText 的教程以及如何绘制文本,并在我的 customView 中实现了这个功能。
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
let path = CGMutablePath()
path.addRect(bounds)
let attrString = NSAttributedString(string: "Hello World")
let framesetter = CTFramesetterCreateWithAttributedString(attrString as CFAttributedString)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil)
CTFrameDraw(frame, context)
}
Run Code Online (Sandbox Code Playgroud)
当文本很短时它工作正常,但当文本超过 10k 个字母时,它会以错误的方式呈现。有什么解决办法吗?
注意:当文本为阿拉伯语而不是英语时会发生这种情况。
这是当文本数量超过 10kb 时的文本渲染,它显得脱节和颠倒:
请怎样才能得到法国信的特定阿拉伯语的路径?我刚刚发现CTFontCreatePathForGlyph会给CGPathRef,但它将是文本的轮廓.
我需要这个真正的文本路径来显示文本绘制动画..
请帮忙
我正在制作一本带有Core Text的杂志,我正在尝试自动为文本添加连字符.我想我可以用这个功能做到这一点
CFStringGetHyphenationLocationBeforeIndex
Run Code Online (Sandbox Code Playgroud)
但它没有用,我在网上找不到任何例子.
我要做的是设置文本,如果我找到任何连字符位置,我在那里放一个" - "并通过访问器替换文本,所以它调用setNeedsDisplay并从头开始绘制.
- (void) setTexto:(NSAttributedString *)texto {
_texto = texto;
if (self.ctFrame != NULL) {
CFRelease(self.ctFrame);
self.ctFrame = NULL;
}
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextScaleCTM(context, 1.0, -1.0);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
CTFramesetterRef ctFrameSetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(_texto));
self.ctFrame = CTFramesetterCreateFrame(ctFrameSetterRef, CFRangeMake(0, [_texto length]), path, NULL);
NSArray *lines = (__bridge NSArray *)(CTFrameGetLines(self.ctFrame));
CFIndex lineCount = [lines count];
NSLog(@"lines: %d", lines.count);
for(CFIndex idx = 0; idx < lineCount; …Run Code Online (Sandbox Code Playgroud) 下面的代码将字符串@"10加到2的幂= 10 [2]"并将红色设置为数字2
问:我想有这种(幻想)效果:

(请注意,我正在寻找一种通用效果,它代表一个小的,稍微高于行数 - 就在一个字符串之后,不一定是"......的力量",但它应该看起来像上图):
-(void)textAttribute
{
NSString *myString = @"ten to the power of two = 10[2]";
NSRange start = [myString rangeOfString:@"["];
NSRange end = [myString rangeOfString:@"]"];
if (start.location != NSNotFound && end.location != NSNotFound && end.location > start.location) {
//NSString *betweenBraces = [myString substringWithRange:NSMakeRange(start.location+1, end.location-(start.location+1))];
NSRange myRange = NSMakeRange(start.location+1, end.location-(start.location+1));
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:myString];
UIColor *powerColor = [UIColor redColor];
[s addAttribute:NSForegroundColorAttributeName value:powerColor range:myRange];
triggerLabel.text = [NSString stringWithFormat:@"Trigger words:%@",powerColor];
triggerLabel.attributedText = s;
}
}
Run Code Online (Sandbox Code Playgroud) 出于某种原因,当使用核心文本时,我无法获得要绘制的NSTextAttachment图像,尽管当NSAttributedString添加到UILabel时,相同的图像会显示正常.
在iOS上,此渲染将为NSTextAttachments提供空白空间,对于OS X,将为每个NSTextAttachment呈现占位符[OBJECT]方形图像.为了使用CoreText渲染图像,还需要做些什么吗?
渲染代码:
CGFloat contextHeight = CGBitmapContextGetHeight(context);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributedString);
CGPathRef path = CGPathCreateWithRect(CGRectMake(rect.origin.x,
contextHeight - rect.origin.y - rect.size.height,
rect.size.width,
rect.size.height), NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CGPathRelease(path);
CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0.0f, contextHeight);
CGContextScaleCTM(context, 1.0f, -1.0f);
CTFrameDraw(frame, context);
CGContextRestoreGState(context);
CFRelease(frame);
Run Code Online (Sandbox Code Playgroud)