Ios 曲线文本起点
我用它来生成曲线文本:Ios 曲线文本起点,ios,nsstring,Ios,Nsstring,我用它来生成曲线文本: - (UIImage*)createCircularText:(NSString*)text withSize:(CGSize)size andCenter:(CGPoint)center { UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; // Start drawing UIGraphicsBeginImageContext(si
- (UIImage*)createCircularText:(NSString*)text withSize:(CGSize)size andCenter:(CGPoint)center
{
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
// Start drawing
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Retrieve the center and set a radius
CGFloat r = center.x / 3;
// Start by adjusting the context origin
// This affects all subsequent operations
CGContextTranslateCTM(context, center.x, center.y);
// Calculate the full extent
CGFloat fullSize = 0;
for (int i = 0; i < [text length]; i++)
{
NSString *letter = [text substringWithRange:NSMakeRange(i, 1)];
CGSize letterSize = [letter sizeWithAttributes:@{NSFontAttributeName:font}];
fullSize += letterSize.width;
}
// Initialize the consumed space
CGFloat consumedSize = 0.0f;
// Iterate through the alphabet
for (int i = 0; i < [text length]; i++)
{
// Retrieve the letter and measure its display size
NSString *letter = [text substringWithRange:NSMakeRange(i, 1)];
CGSize letterSize = [letter sizeWithAttributes:@{NSFontAttributeName:font}];
// Calculate the current angular offset
//CGFloat theta = i * (2 * M_PI / ((float)[text length] * 3));
// Move the pointer forward, calculating the new percentage of travel along the path
consumedSize += letterSize.width / 2.0f;
CGFloat percent = consumedSize / fullSize;
CGFloat theta = (percent * 2 * M_PI) / ((float)[text length] / 4);
consumedSize += letterSize.width / 2.0f;
// Encapsulate each stage of the drawing
CGContextSaveGState(context);
// Rotate the context
CGContextRotateCTM(context, theta);
// Translate up to the edge of the radius and move left by
// half the letter width. The height translation is negative
// as this drawing sequence uses the UIKit coordinate system.
// Transformations that move up go to lower y values.
CGContextTranslateCTM(context, -letterSize.width / 2, -r);
// Draw the letter and pop the transform state
[letter drawAtPoint:CGPointMake(0, 0) withAttributes:@{NSFontAttributeName:font}];
CGContextRestoreGState(context);
}
// Retrieve and return the image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
-(UIImage*)createCircularText:(NSString*)文本带大小:(CGSize)大小和中心:(CGPoint)中心
{
UIFont*font=[UIFont fontWithName:@“直升机驾驶舱灯光”大小:15];
//开始绘图
UIGraphicsBeginImageContext(大小);
CGContextRef context=UIGraphicsGetCurrentContext();
//检索中心并设置半径
cgr=中心x/3;
//从调整上下文原点开始
//这将影响所有后续操作
CGContextTranslateCm(上下文,center.x,center.y);
//计算整个范围
CGFloat fullSize=0;
对于(int i=0;i<[文本长度];i++)
{
NSString*字母=[文本子字符串带范围:NSMakeRange(i,1)];
CGSize LETTESSIZE=[字母大小属性:@{NSFontAttributeName:font}];
全尺寸+=字体尺寸.宽度;
}
//初始化消耗的空间
CGFloat consumedSize=0.0f;
//反复阅读字母表
对于(int i=0;i<[文本长度];i++)
{
//检索字母并测量其显示大小
NSString*字母=[文本子字符串带范围:NSMakeRange(i,1)];
CGSize LETTESSIZE=[字母大小属性:@{NSFontAttributeName:font}];
//计算当前的角度偏移
//CGFloat theta=i*(2*M_PI/((float)[文本长度]*3));
//向前移动指针,计算沿路径移动的新百分比
consumedSize+=字体大小.宽度/2.0f;
CGFloat百分比=消耗尺寸/全尺寸;
CGFloatθ=(百分比*2*M_π)/(浮动)[文本长度]/4);
consumedSize+=字体大小.宽度/2.0f;
//封装绘图的每个阶段
CGContextSaveGState(上下文);
//旋转上下文
CGContextRotateCTM(上下文,θ);
//向上平移到半径的边缘并向左移动
//字母宽度的一半。高度翻译为负数
//因为此绘图序列使用UIKit坐标系。
//向上移动的变换将转到较低的y值。
CGContextTranslateCm(上下文,-lettsize.width/2,-r);
//绘制字母并弹出变换状态
[字母drawAtPoint:CGPointMake(0,0)with attributes:@{NSFontAttributeName:font}];
CGContextRestoreGState(上下文);
}
//检索并返回图像
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回图像;
}
我得到的是:
问题是文本从0°开始,但实际上我希望它从左边开始,这样字符串的中心就在0°。如何做到这一点?有两个选项可以使用:
为了什么?我提出了两个建议。这两个都是对现有代码的微小添加。我在末尾尝试了CGContextRotateCTM(在for循环之外),但什么都没有发生