我使用核心文本API在UI上绘制文本。(我不知道其他方法,因为大多数Google搜索结果都会引导我找到核心文本api)。
我在网上读过一些关于使用核心文本API的教程。在这些教程中,我看到一步一步地从矩阵到配置,属性字符串到框架.但没有一个能详细解释每一步的意义,所以我无法自己去修改。
下面的代码是函数,用(x,y)
绘制屏幕上的文本是我想要绘制的位置。这段代码描述清楚,一步一步地在屏幕上画。尽管如此,我不知道在哪里放置x和y参数,因此文本将开始在这一点上绘制矩形。
// 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
--这似乎是我所需要的,但是我应该把上面的代码放在哪里。我试过一些,但没有成功。请告诉我怎么解决这个问题。
谢谢:)
发布于 2014-08-11 11:02:38
这不是CoreText解决方案,但在您的问题下的评论之后,我认为这将是一个正确的答案。
在视图上绘制基本文本。
.h
#import <UIKit/UIKit.h>
@interface aView : UIView
@end
.m
#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
发布于 2014-08-11 10:34:34
从IOS 7开始,您可以将str呈现到当前上下文中。若要执行此导入UIKit,请执行以下操作。这将扩展NSAttributedString与drawAtPoint:方法的防御。例如,您可能会这样做:
[str drawAtPoint:CGPointMake(x,y)]
有关此方法和其他相关方法的更多信息,请参见以下内容:ref/occ/instm/NSAttributedString/drawAtPoint
发布于 2020-02-09 23:37:15
在“绘制文本”选定的视图中,您可以使用UIView触摸事件,如下所示。为Touch起点和Touch端点创建全局变量,如下所示
CGPoint startingPoint;
CGPoint endingPoint;
然后绘制文本或线条如下
#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];
}
https://stackoverflow.com/questions/25249860
复制