iOS 7 UITextView中的换行符打破了Text Kit排除区域换行 [英] Newlines in iOS 7 UITextView breaking Text Kit exclusion zone wrapping

查看:966
本文介绍了iOS 7 UITextView中的换行符打破了Text Kit排除区域换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 7中的Text Kit,我在 NSTextContainer 禁区附近发现了很多奇怪的事情。

I'm working with Text Kit in iOS 7 and I'm finding a lot of oddities around NSTextContainer exclusion zones.

我有两个观点:一个 UITextView 和一个简单的可拖动 UIView ;当 UIView 移动时,我从 UIView 的框架创建一个bezier路径(调整到<$ c内) $ c> UITextView 的坐标空间)我更新 UITextView NSTextContainer exclusionPaths 数组 - 非常简单。

I've got two views: a UITextView and a simple draggable UIView; as the UIView moves, I create a bezier path from the UIView's frame (adjusted to within the UITextView's coordinate space) and I update the UITextView's NSTextContainer's exclusionPaths array - pretty straightforward.

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

In the first screenshot, you can see that Text Kit nicely wraps text around a rectangular exclusion zone:

然而,当用户将新行引入 UITextView 时,TextKit似乎认为排除区域垂直大得多 - 看起来与换行创建的空白一样高。 bezier路径完全相同,所以这似乎是一个Text Kit问题(除非我做错了)。

However, when the user introduces newlines into the UITextView, TextKit seems to think that the exclusion zone is much bigger vertically - by what appears to be exactly as high as the whitespace created by the newline. The bezier path is exactly the same, so this seems to be a Text Kit issue (unless I'm doing something wrong).

代码:

ViewController.h:

@interface ViewController : UIViewController<UITextViewDelegate>

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

@end

ViewController.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];
}

有什么想法吗?

推荐答案

我今天遇到了同样的问题。

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.




如果您需要这两个选项,只需在代码中设置它们。

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

_textView.textContainer.exclusionPaths = exclusionPaths;
_textView.attributedText = attrString;
_textView.editable = YES;
_textView.selectable = YES;

这篇关于iOS 7 UITextView中的换行符打破了Text Kit排除区域换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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