- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有一个点数组,这些点形成一条线和一个文本。我怎样才能沿着这条线绘制文本
- (void)drawRect:(CGRect)rect
一个 UIView?
我可以毫无问题地绘制路径。是否有我忽略的标准方法或允许我沿着该路径绘制文本的框架?理想情况下,我想只使用 QuartzCore/CoreGraphics 来做到这一点。
我尝试计算每个字符的宽度并旋转每个字符。这种方法可行,但我想知道是否有更优雅的方法。
最佳答案
我相信您可以在 Mac OS X 中执行此操作,但在 iPhone 上最接近的是 CGContextShowGlyphsWithAdvances,它甚至不会旋转。
使用循环并使用类似以下内容绘制每个字符应该不会太难。这是改编自 Apple 的文档且未经测试,因此请注意:
CGContextSelectFont(myContext, "Helvetica", 12, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(myContext, 10);
CGContextSetTextDrawingMode(myContext, kCGTextFillStroke);
CGContextSetRGBFillColor(myContext, 0, 0, 0, 1);
CGContextSetRGBStrokeColor(myContext, 0, 0, 0, 1);
NSUInteger charIndex = 0;
for(NSString *myChar in arrayOfChars) {
char *cString = [myChar UTF8String];
CGPoint charOrigin = originForPositionAlongPath(charIndex, myPath);
CGFloat slope = slopeForPositionAlongPath(charIndex, myPath);
CGContextSetTextMatrix(myContext, CGAffineTransformMakeRotation(slope));
CGContextShowTextAtPoint(myContext, charOrigin.x, charOrigin.y, cString, 1);
}
编辑:这是 PositionAlongPath 函数的概念。同样,它们没有经过测试,但应该接近。 originAlong...如果您用完路径,则返回 (-1, -1)。
CGPoint originForPositionAlongPath(int index, NSArray *path) {
CGFloat charWidth = 12.0;
CGFloat widthToGo = charWidth * index;
NSInteger i = 0;
CGPoint position = [path objectAtIndex:i];
while(widthToGo >= 0) {
//out of path, return invalid point
if(i >= [path count]) {
return CGPointMake(-1, -1);
}
CGPoint next = [path objectAtIndex:i+1];
CGFloat xDiff = next.x - position.x;
CGFloat yDiff = next.y - position.y;
CGFloat distToNext = sqrt(xDiff*xDiff + yDiff*yDiff);
CGFloat fracToGo = widthToGo/distToNext
//point is before next point in path, interpolate the answer
if(fracToGo < 1) {
return CGPointMake(position.x+(xDiff*fracToGo), position.y+(yDiff*fracToGo));
}
//advance to next point on the path
widthToGo -= distToNext;
position = next;
++i;
}
}
CGFloat slopeForPositionAlongPath(int index, NSArray *path) {
CGPoint begin = originForPositionAlongPath(index, path);
CGPoint end = originForPositionAlongPath(index+1, path);
CGFloat xDiff = end.x - begin.x;
CGFloat yDiff = end.y - begin.y;
return atan(yDiff/xDiff);
}
关于objective-c - 在 QuartzCore 中沿路径绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2067166/
我正在尝试使用 QuartzCore Framework,但是当将此框架添加到我的 iPhone 应用程序时,我被告知文件(例如 CIColor.h 等)丢失,但这些丢失的文件存在于我的硬盘驱动器上的
我正在尝试绘制一个半透明的黑色矩形,并从中心切出一个三角形。 我用下面的代码完美地工作。但是,我想圆角三角形的角。 我尝试了几种不同的技术来添加圆角,例如将 CGContextSetLineCap 设
我环顾四周,找不到办法,基本上我想做形状动画,如下图所示。这实际上可以用 iOS 6 API 甚至 iOS 7 API 来完成吗? CAAnimation 中的所有内容似乎都是将整个层移动到另一个 C
所以基本上我正在尝试计算 PDF 的页数,但我遇到了一点麻烦......我导入了 QuartzCore 以使用 PDFKit 计算页数。 我得到的错误是 PDFDocument is an unres
当我尝试为 View 添加边框时,我的 View 变得很慢 [self.userView setBackgroundColor:[UIColor whiteColor]]; [self.userVie
当我在 xcode 内存工具中查看应用程序的内存时,我发现名为 QuartzCore 的东西多次被报告为泄漏。每次泄漏的范围为 16-80 字节。 我的代码没有引用这个。这一切都来自 UIApplic
我将 QuartzCore 框架添加到我的 ios 5.0 项目中,然后尝试 [self layer] setCornerRadius:10.0f]; View 没有任何变化;它仍然是一个矩形。我应该
我在我的 iOS 应用程序中使用 Quartz 核心框架已经有一段时间了。就在最近,在没有对框架进行任何更改的情况下,它导致了我的应用程序中的构建错误。在CATransform3d.h头文件中,发现了
我有一个重播级别按钮。当用户按下此重播级别按钮时,我重置所有游戏属性以重置级别。但是,这还不够。我还想让关卡看起来像是全新的,所以我想向 View Controller 添加动画,就像模态转场的交叉溶
只是一个简单的问题。 有一个简单的可视化工具可以生成iOS/QuartzCore的源代码吗? 例如,我会制作一个带有路径和a的CAKeyframeAnimation(例如CGPathMoveToPoi
我正在使用XYPieChart library绘制我的饼图。我不需要切片选择,所以我手动禁用了它,而且我将始终有 3 个切片。 这就是我的图表现在的样子: 它工作正常,但我希望它看起来像这样: 这是我
我正在实现一个自定义 UIView 以显示在 UIScrollview 中。问题是我需要 View 来投影,所以我做了: #import @implementation CustomView -(v
我一辈子都弄不明白为什么这段代码不起作用: @username_textfield = UITextField.alloc.initWithFrame([[50,50],[50,50]]) @user
我正在运行 Instruments Tool(泄漏)并在 QuartzCore 库中收到内存泄漏,并且不知道我的代码中发生这种情况的确切位置。通常我能够查明发生泄漏的线路,但是仪器工具无法让我在任何地
假设我有一个点数组,这些点形成一条线和一个文本。我怎样才能沿着这条线绘制文本 - (void)drawRect:(CGRect)rect 一个 UIView? 我可以毫无问题地绘制路径。是否有我忽略
这个问题在这里已经有了答案: Quartz Core Import not required in Xcode 5? (1 个回答) 关闭 8 年前。 如果 CALayer header 是 Qua
我有一个关于 Xcode 的问题。我在网上找到了一个教程,可以让我在屏幕上移动图像。你能解释一下我如何让我的按钮移动到左边然后回到右边一次点击(我在任何地方都找不到这个......),然后立即运行另一
我创建了一个应用程序,因此我需要 QuartzCore.framework 来添加到我的项目中,但是在框架列表中搜索时却找不到。这是我的项目: 请告诉我如何在 xcode 中添加这个框架。 (我的xc
我是 iOS 开发的新手,正在尝试为该设备构建应用程序。它给我错误“找不到 QuartzCore.h”。然而,当我尝试为模拟器构建相同的东西时,它工作正常。 我尝试在构建阶段选项卡中添加 Quartz
这个问题在这里已经有了答案: Why masksToBounds = YES prevents CALayer shadow? (6 个答案) 关闭 5 年前。 我使用以下代码为我的 UITextV
我是一名优秀的程序员,十分优秀!