首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS:在矩形中的一个点绘制文本

iOS:在矩形中的一个点绘制文本
EN

Stack Overflow用户
提问于 2014-08-11 18:19:36
回答 4查看 15.1K关注 0票数 5

我使用核心文本API在UI上绘制文本。(我不知道其他方法,因为大多数Google搜索结果都会引导我找到核心文本api)。

我在网上读过一些关于使用核心文本API的教程。在这些教程中,我看到一步一步地从矩阵到配置,属性字符串到框架.但没有一个能详细解释每一步的意义,所以我无法自己去修改。

下面的代码是函数,用(x,y)绘制屏幕上的文本是我想要绘制的位置。这段代码描述清楚,一步一步地在屏幕上画。尽管如此,我不知道在哪里放置x和y参数,因此文本将开始在这一点上绘制矩形。

代码语言:javascript
运行
复制
// draw text on screen.
+ (void)drawText:(CGContextRef)context bound:(CGRect)rect text:(NSString *)text x:(float)x y:(float) y color:(UIColor *)color size:(float)textSize{
    CGContextSaveGState(context);
    // always remember to reset the text matrix before drawing.
    // otherwise the result will be unpredictable like using uninitialize memory
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0f, -1.0f));
    // Flip the coordinate system. because core Text uses different coordinate system with UI Kit
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // step 1: prepare attribute string
    NSDictionary *attributes;
    attributes = @{
            (NSString *) kCTFontAttributeName            : [UIFont fontWithName:@"Helvetica" size:textSize],
            (NSString *) kCTForegroundColorAttributeName : color
    };

    NSAttributedString *str = [[NSAttributedString alloc]
                               initWithString:text attributes:attributes];

    CFAttributedStringRef attrString = (__bridge CFAttributedStringRef)str;

    // step 2: create CTFFrameSetter
    CTFramesetterRef framesetter =
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);

    // step 3. create CGPath
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect);

    // step 4. get the frame
    // use the CGPath and CTFrameSetter to create CTFrame. then drawn it in currently context
    CTFrameRef frame = CTFramesetterCreateFrame
            (framesetter, CFRangeMake(0, 0), path, NULL);

    CTFrameDraw(frame, context);

    // step 5. release resource
    //CFRelease(attrString);
    CFRelease(framesetter);
    CFRelease(frame);
    CFRelease(path);

    CGContextRestoreGState(context);
}

而且,我看到了一些函数:CGContextSetTextPosition --这似乎是我所需要的,但是我应该把上面的代码放在哪里。我试过一些,但没有成功。请告诉我怎么解决这个问题。

谢谢:)

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-08-11 19:02:38

这不是CoreText解决方案,但在您的问题下的评论之后,我认为这将是一个正确的答案。

在视图上绘制基本文本。

.h

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>

@interface aView : UIView
@end

.m

代码语言:javascript
运行
复制
#import "aView.h"

@implementation aView

- (void)drawText:(CGFloat)xPosition yPosition:(CGFloat)yPosition canvasWidth:(CGFloat)canvasWidth canvasHeight:(CGFloat)canvasHeight
{
    //Draw Text 
    CGRect textRect = CGRectMake(xPosition, yPosition, canvasWidth, canvasHeight);
    NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    textStyle.alignment = NSTextAlignmentLeft;

    NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: @"Helvetica" size: 12], NSForegroundColorAttributeName: UIColor.redColor, NSParagraphStyleAttributeName: textStyle};

    [@"Hello, World!" drawInRect: textRect withAttributes: textFontAttributes];
}


- (void)drawRect:(CGRect)rect {
    [self drawText:0 yPosition:0 canvasWidth:200 canvasHeight:150];
}

@end
票数 12
EN

Stack Overflow用户

发布于 2014-08-11 18:34:34

从IOS 7开始,您可以将str呈现到当前上下文中。若要执行此导入UIKit,请执行以下操作。这将扩展NSAttributedString与drawAtPoint:方法的防御。例如,您可能会这样做:

代码语言:javascript
运行
复制
[str drawAtPoint:CGPointMake(x,y)]

有关此方法和其他相关方法的更多信息,请参见以下内容:ref/occ/instm/NSAttributedString/drawAtPoint

票数 3
EN

Stack Overflow用户

发布于 2020-02-10 07:37:15

在“绘制文本”选定的视图中,您可以使用UIView触摸事件,如下所示。为Touch起点和Touch端点创建全局变量,如下所示

代码语言:javascript
运行
复制
CGPoint startingPoint;
CGPoint endingPoint;

然后绘制文本或线条如下

代码语言:javascript
运行
复制
#pragma mark- Touch Event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [[event allTouches] anyObject];
   startingPoint = [touch locationInView:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   UITouch *touch = [touches anyObject];
   endingPoint = [touch locationInView:self];
   [self makeLineLayer:self.layer lineFromPointA:startingPoint 
   toPointB:endingPoint];
}

-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA 
          toPointB:(CGPoint)pointB
{
   CAShapeLayer *line = [CAShapeLayer layer];
   UIBezierPath *linePath=[UIBezierPath bezierPath];
   [linePath moveToPoint: pointA];
   [linePath addLineToPoint:pointB];
   line.path=linePath.CGPath;
   line.fillColor = nil;
   line.opacity = 2.0;
   line.lineWidth = 4.0;
   line.strokeColor = [UIColor redColor].CGColor;
   [layer addSublayer:line];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25249860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档