NSTextContainer的例子有非规则形状? [英] Example of NSTextContainer with non regular shape?

查看:231
本文介绍了NSTextContainer的例子有非规则形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的 TextKit API for iOS7,我试图产生一个 UITextView 具有不规则形状。到目前为止我在一个视图控制器:

   - (void)loadView 
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,548)];

NSTextStorage * textStorage = [[NSTextStorage alloc] init];

NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];

BaseTextContainer * textContainer = [[BaseTextContainer alloc] initWithSize:CGSizeMake(100,100)];
[layoutManager addTextContainer:textContainer];

BaseTextView * textView = [[BaseTextView alloc] initWithFrame:CGRectMake(110,124,100,100)textContainer:textContainer];
textView.backgroundColor = [UIColor blueColor];
textView.editable = YES;
[self.view addSubview:textView];
}

然后在我的子类 NSTextContainer ,我想有一个 mutablePath 绘制为文本容器的形状,但不知道如何完成这个。我有:

   - (BOOL)isSimpleRectangularTextContainer 
{
return NO;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSLog(@TEST);
CGContextRef context = ctx;
CGSize layerSize = layer.frame.size;

CGAffineTransform transform = CGAffineTransformMakeScale(layerSize.width / self.initialSize.width,layerSize.height / self.initialSize.height);
CGMutablePathRef newGraphicMutablePath = CGPathCreateMutableCopyByTransformingPath(self.mutablePath,& transform);
CGContextAddPath(context,newGraphicMutablePath);

CGPathRelease(newGraphicMutablePath);
CGContextSetFillColorWithColor(context,[UIColor redColor] .CGColor);
CGContextDrawPath(context,kCGPathFill);
}

只是有点困惑,如何让这个工作。我找不到一个例子任何地方的 NSTextContainer 与不规则形状。

解决方案

无需构建Text Kit堆栈的所有代码,因为您不会修改堆栈的架构。只需从一个普通的UITextView开始 - 让我们说它 self.textView - 然后将一个或多个UIBezierPath对象分配给它的排除路径:

  self.tv.textContainer.exclusionPaths = myArrayOfBezierPaths; 

这些路径是排除路径,因此,



或者,您可以自己构建Text Kit堆栈,以便插入您自己的文本容器子类,并且通过重写 lineFragmentForProposedRect:修改文本的位置,可能类似于我在这里: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p537exclusionPath2/ ch23p813textKitShapes / MyTextContainer.swift



一些实验:






Hi I'm working with the new TextKit API for iOS7 and I'm trying to produce a UITextView with an irregular shape. So far I have in a view controller:

-(void) loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,548)];

    NSTextStorage *textStorage = [[NSTextStorage alloc] init];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager: layoutManager];

    BaseTextContainer *textContainer = [[BaseTextContainer alloc] initWithSize:CGSizeMake(100, 100)];
    [layoutManager addTextContainer: textContainer];

    BaseTextView *textView = [[BaseTextView alloc] initWithFrame:CGRectMake(110,124, 100, 100) textContainer:textContainer];
    textView.backgroundColor = [UIColor blueColor];
    textView.editable = YES;
    [self.view addSubview:textView];
}

Then in my subclassed NSTextContainer, I want to have a mutablePath drawn as the shape of the text container, but not sure how to accomplish this. I have:

- (BOOL) isSimpleRectangularTextContainer
{
    return NO;
}

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSLog(@"TEST");
    CGContextRef context = ctx;
    CGSize layerSize = layer.frame.size;

    CGAffineTransform transform = CGAffineTransformMakeScale(layerSize.width / self.initialSize.width, layerSize.height / self.initialSize.height);
    CGMutablePathRef newGraphicMutablePath = CGPathCreateMutableCopyByTransformingPath(self.mutablePath, &transform);
    CGContextAddPath(context, newGraphicMutablePath);

    CGPathRelease(newGraphicMutablePath);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextDrawPath(context, kCGPathFill);
}

Just a bit confused about how to get this to work. I cannot find an example anywhere of an NSTextContainer with an irregular shape.

解决方案

There is no need for all that code constructing the Text Kit stack, as you are not modifying the architecture of the stack. Just start with a normal UITextView - let's say it's self.textView - and then assign one or more UIBezierPath objects to its exclusion paths:

self.tv.textContainer.exclusionPaths = myArrayOfBezierPaths;

These paths are exclusion paths, so for an ellipse you will want to make four paths, each one describing a corner of the text container.

Alternatively, you can build the Text Kit stack yourself so as to insert your own text container subclass, and modify where the text is allowed to go by overriding lineFragmentForProposedRect:, perhaps similar to what I do here: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p537exclusionPath2/ch23p813textKitShapes/MyTextContainer.swift

Some experiments:

这篇关于NSTextContainer的例子有非规则形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆