这个怎么画啊cad弧形楼梯怎么画

cad中怎么画弧形? cad3点画弧线的方法
[摘要]本经验来介绍下cad中3点画弧的方法,简单步骤如下:1、如图所示,有三个已知点2、输入画弧命令&a&,回车。3、指定...
本经验来介绍下cad中3点画弧的方法,简单步骤如下:
1、如图所示,有三个已知点
2、输入画弧命令&a&,回车。
3、指定弧线起点。
4、指定弧线第二个点。
5、指点弧线迪三个点。
6、完成,可以得到一段弧线。
三维子分类【iOS开发UI篇】绘制弧形文字 - 简书
【iOS开发UI篇】绘制弧形文字
本文介绍了使用苹果提供的CoreText框架绘制出弧形效果文字的方法
CoreText是由苹果官方提供的文本引擎,它提供了多种控制文字布局的方式,通过使用CoreText框架可以控制文字的位置、颜色、尺寸等
苹果官方提供了一段可以绘制弧形文字的代码,使用起来非常方便,地址是:
注意:本文中我们将修改CoreTextArcView.m中的部分代码,使得调用接口所得到的图形更容易理解和辨识(源代码调试起来比较花时间,这里将不做对比,只陈述修改后的调试策略,本篇代码改动纯属个人喜好)
首先我们在工程中创建新的文件,这里我们就命名为CoreTextArcView,并将以下代码导入到.h和.m文件中:
CoreTextArcView.h
#import &UIKit/UIKit.h&
#import &CoreText/CoreText.h&
@interface CoreTextArcView : UIView {
NSString *
_shiftH, _shiftV; // horiz & vertical shift
unsigned int
showsGlyphBounds:1;
unsigned int
showsLineMetrics:1;
unsigned int
dimsSubstitutedGlyphs:1;
unsigned int
reserved:29;
@property(retain, nonatomic) UIFont *
@property(retain, nonatomic) NSString *
@property(readonly, nonatomic) NSAttributedString *attributedS
@property(assign, nonatomic) CGF
@property(nonatomic) BOOL showsGlyphB
@property(nonatomic) BOOL showsLineM
@property(nonatomic) BOOL dimsSubstitutedG
@property(retain, nonatomic) UIColor *
@property(nonatomic) CGFloat arcS
@property(nonatomic) CGFloat shiftH, shiftV;
- (instancetype)initWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text radius:(float)radius arcSize:(float)arcSize color:(UIColor *)
CoreTextArcView.m
#import "CoreTextArcView.h"
#import &AssertMacros.h&
#import &QuartzCore/QuartzCore.h&
#define ARCVIEW_DEBUG_MODE
#define ARCVIEW_DEFAULT_FONT_NAME
@"Helvetica"
#define ARCVIEW_DEFAULT_FONT_SIZE
#define ARCVIEW_DEFAULT_RADIUS
#define ARCVIEW_DEFAULT_ARC_SIZE
@implementation CoreTextArcView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.font = [UIFont fontWithName:ARCVIEW_DEFAULT_FONT_NAME size:ARCVIEW_DEFAULT_FONT_SIZE];
self.text = @"Curva Style Label";
self.radius = ARCVIEW_DEFAULT_RADIUS;
self.showsGlyphBounds = NO;
self.showsLineMetrics = NO;
self.dimsSubstitutedGlyphs = NO;
self.color = [UIColor grayColor];
self.arcSize = ARCVIEW_DEFAULT_ARC_SIZE;
self.shiftH = self.shiftV = 0.0f;
- (instancetype)initWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text radius:(float)radius arcSize:(float)arcSize color:(UIColor *)color{
self = [super initWithFrame:frame];
if (self) {
self.font =
self.text =
self.radius =
self.showsGlyphBounds = NO;
self.showsLineMetrics = NO;
self.dimsSubstitutedGlyphs = NO;
self.color =
self.arcSize = arcS
self.shiftH = self.shiftV = 0.0f;
typedef struct GlyphArcInfo {
// in radians
} GlyphArcI
static void PrepareGlyphArcInfo(CTLineRef line, CFIndex glyphCount, GlyphArcInfo *glyphArcInfo, CGFloat arcSizeRad)
NSArray *runArray = (NSArray *)CTLineGetGlyphRuns(line);
// Examine each run in the line, updating glyphOffset to track how far along the run is in terms of glyphCount.
CFIndex glyphOffset = 0;
for (id run in runArray) {
CFIndex runGlyphCount = CTRunGetGlyphCount((CTRunRef)run);
// Ask for the width of each glyph in turn.
CFIndex runGlyphIndex = 0;
for (; runGlyphIndex & runGlyphC runGlyphIndex++) {
glyphArcInfo[runGlyphIndex + glyphOffset].width = CTRunGetTypographicBounds((CTRunRef)run, CFRangeMake(runGlyphIndex, 1), NULL, NULL, NULL);
glyphOffset += runGlyphC
double lineLength = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
CGFloat prevHalfWidth = glyphArcInfo[0].width / 2.0;
glyphArcInfo[0].angle = (prevHalfWidth / lineLength) * arcSizeR
// Divide the arc into slices such that each one covers the distance from one glyph's center to the next.
CFIndex lineGlyphIndex = 1;
for (; lineGlyphIndex & glyphC lineGlyphIndex++) {
CGFloat halfWidth = glyphArcInfo[lineGlyphIndex].width / 2.0;
CGFloat prevCenterToCenter = prevHalfWidth + halfW
glyphArcInfo[lineGlyphIndex].angle = (prevCenterToCenter / lineLength) * arcSizeR
prevHalfWidth = halfW
// ensure that redraw occurs.
-(void)setText:(NSString *)text{
_string = [NSString stringWithFormat:@"%@",text];
[self setNeedsDisplay];
//set arc size in degrees (180 = half circle)
-(void)setArcSize:(CGFloat)degrees{
_arcSize = degrees * M_PI/180.0;
//get arc size in degrees
-(CGFloat)arcSize{
return _arcSize * 180.0/M_PI;
- (void)drawRect:(CGRect)rect {
// Don't draw if we don't have a font or string
if (self.font == NULL || self.text == NULL)
// Initialize the text matrix to a known value
CGContextRef context = UIGraphicsGetCurrentContext();
//Reset the transformation
//Doing this means you have to reset the contentScaleFactor to 1.0
CGAffineTransform t0 = CGContextGetCTM(context);
CGFloat xScaleFactor = t0.a & 0 ? t0.a : -t0.a;
CGFloat yScaleFactor = t0.d & 0 ? t0.d : -t0.d;
t0 = CGAffineTransformInvert(t0);
if (xScaleFactor != 1.0 || yScaleFactor != 1.0)
t0 = CGAffineTransformScale(t0, xScaleFactor, yScaleFactor);
CGContextConcatCTM(context, t0);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
if(ARCVIEW_DEBUG_MODE){
// Draw a black background (debug)
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, self.layer.bounds);
NSAttributedString *attStr = self.attributedS
CFAttributedStringRef asr = (__bridge CFAttributedStringRef)attS
CTLineRef line = CTLineCreateWithAttributedString(asr);
assert(line != NULL);
CFIndex glyphCount = CTLineGetGlyphCount(line);
if (glyphCount == 0) {
CFRelease(line);
GlyphArcInfo *
glyphArcInfo = (GlyphArcInfo*)calloc(glyphCount, sizeof(GlyphArcInfo));
PrepareGlyphArcInfo(line, glyphCount, glyphArcInfo, _arcSize);
// Move the origin from the lower left of the view nearer to its center.
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMidX(rect)+_shiftH, CGRectGetMidY(rect)+_shiftV - self.radius / 2.0); //注意这是要修改的一行
if(ARCVIEW_DEBUG_MODE){
// Stroke the arc in red for verification.
CGContextBeginPath(context);
CGContextAddArc(context, 0.0, 0.0, self.radius, M_PI_2+_arcSize/2.0, M_PI_2-_arcSize/2.0, 1);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextStrokePath(context);
// Rotate the context 90 degrees counterclockwise (per 180 degrees)
CGContextRotateCTM(context, _arcSize/2.0);
// Now for the actual drawing. The angle offset for each glyph relative to the previous glyph has alr with that information in hand, draw those glyphs overstruck and centered over one another, making sure to rotate the context after each glyph so the glyphs are spread along a semicircular path.
CGPoint textPosition = CGPointMake(0.0, self.radius);
CGContextSetTextPosition(context, textPosition.x, textPosition.y);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
CFIndex runCount = CFArrayGetCount(runArray);
CFIndex glyphOffset = 0;
CFIndex runIndex = 0;
for (; runIndex & runC runIndex++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CFIndex runGlyphCount = CTRunGetGlyphCount(run);
Boolean drawSubstitutedGlyphsManually =
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
// Determine if we need to draw substituted glyphs manually. Do so if the runFont is not the same as the overall font.
if (self.dimsSubstitutedGlyphs && ![self.font isEqual:(__bridge UIFont *)runFont]) {
drawSubstitutedGlyphsManually =
CFIndex runGlyphIndex = 0;
for (; runGlyphIndex & runGlyphC runGlyphIndex++) {
CFRange glyphRange = CFRangeMake(runGlyphIndex, 1);
CGContextRotateCTM(context, -(glyphArcInfo[runGlyphIndex + glyphOffset].angle));
// Center this glyph by moving left by half its width.
CGFloat glyphWidth = glyphArcInfo[runGlyphIndex + glyphOffset].
CGFloat halfGlyphWidth = glyphWidth / 2.0;
CGPoint positionForThisGlyph = CGPointMake(textPosition.x - halfGlyphWidth, textPosition.y);
// Glyphs are positioned relative to the text position for the line, so offset text position leftwards by this glyph's width in preparation for the next glyph.
textPosition.x -= glyphW
CGAffineTransform textMatrix = CTRunGetTextMatrix(run);
textMatrix.tx = positionForThisGlyph.x;
textMatrix.ty = positionForThisGlyph.y;
CGContextSetTextMatrix(context, textMatrix);
if (!drawSubstitutedGlyphsManually) {
CTRunDraw(run, context, glyphRange);
// We need to draw the glyphs manually in this case because we are effectively applying a graphics operation by setting the context fill color. Normally we would use kCTForegroundColorAttributeName, but this does not apply as we don't know the ranges for the colors in advance, and we wanted demonstrate how to manually draw.
CGFontRef cgFont = CTFontCopyGraphicsFont(runFont, NULL);
CTRunGetGlyphs(run, glyphRange, &glyph);
CTRunGetPositions(run, glyphRange, &position);
CGContextSetFont(context, cgFont);
CGContextSetFontSize(context, CTFontGetSize(runFont));
CGContextSetRGBFillColor(context, 0.25, 0.25, 0.25, 0.5);
CGContextShowGlyphsAtPositions(context, &glyph, &position, 1);
CFRelease(cgFont);
// Draw the glyph bounds
if ((self.showsGlyphBounds) != 0) {
CGRect glyphBounds = CTRunGetImageBounds(run, context, glyphRange);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, glyphBounds);
// Draw the bounding boxes defined by the line metrics
if ((self.showsLineMetrics) != 0) {
CGRect lineM
CGFloat ascent,
CTRunGetTypographicBounds(run, glyphRange, &ascent, &descent, NULL);
// The glyph is centered around the y-axis
lineMetrics.origin.x = -halfGlyphW
lineMetrics.origin.y = positionForThisGlyph.y -
lineMetrics.size.width = glyphW
lineMetrics.size.height = ascent +
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRect(context, lineMetrics);
glyphOffset += runGlyphC
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetAlpha(context,0.0);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
free(glyphArcInfo);
CFRelease(line);
@synthesize font = _
@synthesize text = _
@synthesize radius = _
@synthesize color = _
@synthesize arcSize = _arcS
@synthesize shiftH = _shiftH;
@synthesize shiftV = _shiftV;
@dynamic attributedS
- (NSAttributedString *)attributedString {
// Create an attributed string with the current font and string.
assert(self.font != nil);
assert(self.text != nil);
// Create our attributes...
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
CGColorRef colorRef = self.color.CGC
// pack it into attributes dictionary
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)fontRef, (id)kCTFontAttributeName,
colorRef, (id)kCTForegroundColorAttributeName,
assert(attributesDict != nil);
// Create the attributed string
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.text attributes:attributesDict];
return attrS
@dynamic showsGlyphB
- (BOOL)showsGlyphBounds {
return _flags.showsGlyphB
- (void)setShowsGlyphBounds:(BOOL)show {
_flags.showsGlyphBounds = show ? 1 : 0;
@dynamic showsLineM
- (BOOL)showsLineMetrics {
return _flags.showsLineM
- (void)setShowsLineMetrics:(BOOL)show {
_flags.showsLineMetrics = show ? 1 : 0;
@dynamic dimsSubstitutedG
- (BOOL)dimsSubstitutedGlyphs {
return _flags.dimsSubstitutedG
- (void)setDimsSubstitutedGlyphs:(BOOL)dim {
_flags.dimsSubstitutedGlyphs = dim ? 1 : 0;
这段代码是官方提供的范例,对于我们绘制弧形文字已经足够使用,在此不做过多分析,我们只关注头文件中提供的实例方法即可:
- (instancetype)initWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text radius:(float)radius arcSize:(float)arcSize color:(UIColor *)
通过调用这个方法我们就可以绘制出有弧形效果的文字了,另外还可以为文字修改字体、内容、弧度半径、张角、颜色等,这里我们绘制多个文字进行对比来了解各个参数的意义,尤其是弧度半径radius和张角arcSize两个参数
接下来我们将微调源代码的这一行:
CGContextTranslateCTM(context, CGRectGetMidX(rect)+_shiftH, CGRectGetMidY(rect)+_shiftV - self.radius / 2.0);
这里是在移动参考上下文的原点的位置,可以看出原点的y值还和要绘制的弧度半径相关,我们希望将原点的位置固定(只取决于底版view的尺寸),不受要绘制的文字的参数影响,因此我们需将代码修改为:
CGContextTranslateCTM(context, CGRectGetMidX(rect)+_shiftH, CGRectGetMidY(rect)+_shiftV);
这样就可以了,具体效果后面我们会看到
准备工作就绪,我们将开始绘制多个弧形文字来看下效果和使用方法是怎样的
首先我们创建一个作为背景的UIView对象,这个view的尺寸决定了弧形文字视图的尺寸,view的高度和弧度半径radius有关,view的宽度和张角arcSize有关,详细解释后面会提到
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *arcBgView = [[UIView alloc] init];
arcBgView.bounds = CGRectMake(0, 0, 300, 600);
arcBgView.center = self.view.
arcBgView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:arcBgView];
接下来,在要绘制文字的文件中引入CoreTextArcView.h头文件,并调用接口创建CoreTextArcView实例并添加就可以了,例如
CoreTextArcView *arcViewTest = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径200张角45" radius:200 arcSize:45 color:[UIColor redColor]];
arcViewTest.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcViewTest];
这样就添加了一个弧形文字,效果如下,注意黄色区域为高600宽300,圆弧的中心此时位于黄色区域的正中心,弧度半径为200,略小于高的一半300
弧形文字测试界面
一条文字可能不足以对比出效果,我们固定张角,调整半径,绘制多条文字
CoreTextArcView *arcView1 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度0张角30" radius:0 arcSize:30 color:[UIColor redColor]];
arcView1.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView1];
CoreTextArcView *arcView2 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度50张角30" radius:50 arcSize:30 color:[UIColor redColor]];
arcView2.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView2];
CoreTextArcView *arcView3 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度100张角30" radius:100 arcSize:30 color:[UIColor redColor]];
arcView3.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView3];
CoreTextArcView *arcView4 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度150张角30" radius:150 arcSize:30 color:[UIColor redColor]];
arcView4.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView4];
CoreTextArcView *arcView5 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度200张角30" radius:200 arcSize:30 color:[UIColor redColor]];
arcView5.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView5];
CoreTextArcView *arcView6 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径250张角30" radius:250 arcSize:30 color:[UIColor redColor]];
arcView6.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView6];
CoreTextArcView *arcView7 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径300张角30" radius:300 arcSize:30 color:[UIColor redColor]];
arcView7.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView7];
不同弧度半径弧形文字对比图
这里我们看到几个问题:
第一,最上面一条半径为300的文字已经超出了边框外,这是因为CoreTextArcView在绘制文字时所使用的半径radius,其实是在文字下方首先绘制一条弧线,再将文字贴合弧线外侧绘制,因此本图中当radius已经和黄色区域高度的一半相等时(这里都为300),弧线已经贴到了边框顶部,文字自然就被绘制到边框外了,因此在绘制时我们要注意控制radius略小于设定的frame的高度的一半即可
第二,我们使用了一个很大的边框,但是图形只绘制到了边框内部的上半部分,那么下半部分是不是没有用呢?答案并非如此,因为除了上弧形的文字,我们还可以绘制下弧形的文字,而使用的方法是相似的,只需要将radius和arcSize都变成负值即可
CoreTextArcView *arcView1 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径0张角45" radius:-0 arcSize:-45 color:[UIColor redColor]];
arcView1.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView1];
CoreTextArcView *arcView2 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径50张角45" radius:-50 arcSize:-45 color:[UIColor redColor]];
arcView2.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView2];
CoreTextArcView *arcView3 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径100张角45" radius:-100 arcSize:-45 color:[UIColor redColor]];
arcView3.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView3];
CoreTextArcView *arcView4 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径150张角45" radius:-150 arcSize:-45 color:[UIColor redColor]];
arcView4.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView4];
CoreTextArcView *arcView5 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径200张角45" radius:-200 arcSize:-45 color:[UIColor redColor]];
arcView5.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView5];
CoreTextArcView *arcView6 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径250张角45" radius:-250 arcSize:-45 color:[UIColor redColor]];
arcView6.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView6];
CoreTextArcView *arcView7 = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径300张角45" radius:-300 arcSize:-45 color:[UIColor redColor]];
arcView7.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcView7];
将弧形文字绘制到下方
这里我们看到最外层radius为-300的文字没有绘制到边框外,因为负值半径改变了弧形文字贴合弧形图的方向,也就将文字绘制到了弧形内侧,这里我们可以通过再绘制一个正半径和一个负半径全角度的文字来说明:
//蓝色的是正半径,外圈绘制
CoreTextArcView *arcViewOutFull = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径150张角360弧度半径150张角360弧度半径150张角360" radius:150 arcSize:360 color:[UIColor blueColor]];
arcViewOutFull.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcViewOutFull];
//红色的是负半径,内圈绘制
CoreTextArcView *arcViewInFull = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径-150张角360弧度半径-150张角360弧度半径-150张角360" radius:-150 arcSize:360 color:[UIColor redColor]];
arcViewInFull.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcViewInFull];
正半径与负半径对比图
这样我们就能看出来正负半径带来的绘制差异了,也就是半径正值或负值将决定文字要绘制到弧形图的外侧或者内侧,另外也可以看出,arcSize是用来决定文字展开的角度有多大的,文字将会根据这个角度自动填充间隙,arcSize如果为负值时,也会影响文字的排列顺序,所以需要结合半径是正值还是负值来使用才可以满足不同的需求
到这里为止,利用CoreTextArcView来绘制弧形文字的基本方法就说的差不多了,总结起来步骤大概是这样的:
添加CoreTextArcView文件(可以是其他名称)和代码(根据个人喜好决定是否修改源代码来增强使用效率)
添加一个view作为绘制弧形文字的背景底版,弧形的中心将是该view的中心点,view的宽高设定一定要符合弧形文字的尺寸需要(宽度对应文字的张角arcSize,高度的一半对应文字的弧度半径radius)
调用接口-initWithFrame:font:radius:arcSize:color: 来创建和配置所需要的弧形文字(注意半径和张角的正值或负值将决定文字的绘制方式和样式)
将创建好的弧形文字视图的背景色设为透明色(否则会以饼状形式覆盖住圆心到文字之间的区域!)
添加到底版view上显示即可
在文章开始我们提到过通过修改源代码使得调用接口进行调试的结果变得简单直观,这只是个人喜好,本人没有仔细钻研过苹果提供的源代码,只是通过修改的方式找到了符合自己的更好使用的方法,也就是在设置好参数后,脑海中就已经出现大致的图形了,在使用源代码时这个部分因为添加了self.radius的成分而使得半径的变化掺杂了多余的影响因素,对判断位置有一些干扰,故而做出修改
前面提到过,如果我们只绘制一半弧形文字时,会浪费掉一半底版区域,也会给设置底版view的位置带来一些麻烦,因此我们可以进一步修改,使得源代码能够在绘制上弧形文字时,将弧形中心移到view底部中心,在绘制下弧形文字时,将弧形中心移到view顶部中心,这样既可以不必创建过大的底版view,也可以在将view添加到其他view上进行组合时,能更好的以弧形圆心为参照进行调整
这里我们整理三种绘制方式需要修改的代码细节:
// 弧形圆心位于底版view正中心,适用于所有绘制情况,尤其是弧形文字张角大于180度的情况(比如上面绘制一整个圆形)或者弧形文字跨越水平线(左弧形或右弧形)
CGContextTranslateCTM(context, CGRectGetMidX(rect)+_shiftH, CGRectGetMidY(rect)+_shiftV);
// 弧形圆心位于底版view底部中心,适用于上弧形
CGContextTranslateCTM(context, CGRectGetMidX(rect)+_shiftH, _shiftV);
// 弧形圆心位于底版view顶部中心,使用与下弧形
CGContextTranslateCTM(context, CGRectGetMidX(rect)+2*CGRectGetMidY(rect)_shiftH, -_shiftV);
我们添加一段测试代码来检测一下,首先是上弧形文字,注意半径略小于view的高度才能完整显示
CoreTextArcView *arcViewTopTest = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径280张角45" radius:280 arcSize:45 color:[UIColor redColor]];
arcViewTopTest.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcViewTopTest];
上弧形文字示例
接下来是下弧形文字
CoreTextArcView *arcViewBottomTest = [[CoreTextArcView alloc] initWithFrame:arcBgView.bounds font:[UIFont systemFontOfSize:20] text:@"弧度半径300张角45" radius:-300 arcSize:-45 color:[UIColor redColor]];
arcViewBottomTest.backgroundColor = [UIColor clearColor];
[arcBgView addSubview:arcViewBottomTest];
下弧形文字示例
简单的变换使用就是这些了,除此之外还有其他很多种变化方法可以尝试
【参考资料】Github - Apple CoreText Framework CoreTextArcViewGithub - XMCircleTypestack overflow - Curve text on existing circle
iOS开发初学者一枚

我要回帖

更多关于 广联达弧形梁怎么画 的文章

 

随机推荐