5

我正在使用 iOS 7 中的 Text Kit,并且在NSTextContainer禁区周围发现了很多奇怪的东西。

我有两个视图:一个UITextView和一个简单的 draggable UIView;随着UIView移动,我从 ' 框架创建一个贝塞尔路径UIView(调整到UITextView' 坐标空间内)并更新UITextView' NSTextContainer'exclusionPaths数组 - 非常简单。

在第一个屏幕截图中,您可以看到 Text Kit 很好地将文本包裹在一个矩形禁区内:

文本中没有换行符的排除区域

但是,当用户将换行符引入 时UITextView,TextKit 似乎认为禁区在垂直方向上要大得多 - 似乎与换行符创建的空白一样高。贝塞尔路径完全相同,所以这似乎是一个文本工具包问题(除非我做错了什么)。

文本中带有换行符的禁区

代码:

视图控制器.h:

@interface ViewController : UIViewController<UITextViewDelegate>

@property (nonatomic, strong) IBOutlet UITextView *textView;
@property (nonatomic, strong) IBOutlet UIView *dragView;

@end

视图控制器.m:

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [self.dragView addGestureRecognizer:panRecognizer];

    [self updateExclusionZone];
}

-(void)move:(UIPanGestureRecognizer *)pan
{
    [self.view bringSubviewToFront:[pan view]];

    if ([pan state] == UIGestureRecognizerStateBegan) {
        NSLog(@"pan began");
    }

    self.dragView.center = [pan locationInView:self.view];
    [self updateExclusionZone];

    if ([pan state] == UIGestureRecognizerStateEnded) {
        NSLog(@"pan ended");
    }
}

-(void)updateExclusionZone
{
    CGRect dragViewFrame = self.dragView.frame;
    CGRect exclusionRect = [self.view convertRect:dragViewFrame toView:self.textView];

    UIBezierPath *exclusion = [UIBezierPath bezierPathWithRect:exclusionRect];

    self.textView.textContainer.exclusionPaths = @[exclusion];
}

有什么想法吗?

4

1 回答 1

5

I ran into the same issue today.

This bug seem to appear, if you set editable and selectable at the same time. If only one or no is selected, it renders as expected. Both are selected by default.

enter image description here
enter image description here

If you need both options, just set them in code.

_textView.textContainer.exclusionPaths = exclusionPaths;
_textView.attributedText = attrString;
_textView.editable = YES;
_textView.selectable = YES;
于 2013-11-01T03:15:51.177 回答