ios - 具有非规则形状的 NSTextContainer 示例?

标签 ios cocoa-touch uitextview textkit

您好,我正在使用适用于 iOS7 的新 TextKit API,我正在尝试生成一个具有不规则形状的 UITextView。到目前为止,我在 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];
}

然后在我的子类 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

一些实验:

enter image description here

enter image description here

关于ios - 具有非规则形状的 NSTextContainer 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23285721/

相关文章:

ios - 清除时在 textView 中重置 attributedText 的属性 - Swift

ios - 删除存储在 nsuserdefault 中的 nsdata

iphone - Facebook SDK 和 iOS 应用程序 (authButtonAction) 的问题

ios - UITextView/UITextField 的虚线/虚线边框

ios - 我可以在 iPhone 应用程序中嵌入自定义字体吗?

ios - RxSwift Disposable property isDisposed always false or Disposable closure newer called to cancel worker task

ios - 向已填充的 UITextView 添加文本

ios - UIImage 返回 nil 但存在

javascript - 检测 UIWebView 中的 Javascript/Ajax 更改

java - 如何使用 JSP/Java 检测移动设备(iOS 和 Android)?