objective-c - iOS 7 UITextView 中的换行符打破了 Text Kit 禁区换行

标签 objective-c ipad ios7 textkit

我在 iOS 7 中使用 Text Kit,我发现 NSTextContainer 禁区有很多奇怪之处。

我有两个 View :一个UITextView 和一个简单的可拖动UIView;随着 UIView 的移动,我从 UIView 的框架(调整到 UITextView 的坐标空间内)创建了一条贝塞尔曲线路径,然后我更新 UITextViewNSTextContainerexclusionPaths 数组 - 非常简单。

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

Exclusion zone with no newlines in text

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

Exclusion zone with newlines in text

代码:

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

有什么想法吗?

最佳答案

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

如果您同时设置editableselectable,似乎会出现此错误。如果只选择一个或没有选择,它将按预期呈现。两者都是默认选中的。

enter image description here
enter image description here

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

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

关于objective-c - iOS 7 UITextView 中的换行符打破了 Text Kit 禁区换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963648/

相关文章:

ios - UIActionSheet 在 iOS 7 上呈现时显示黑线

ios - 图层 maskToBounds 默认值不起作用

ios - CABasicAnimation 的 toValue 是否相对于初始位置?

ios - 在 subview removeFromSuperview 之后执行操作(Ios Swift)

iphone - 将变量从一个 View 转移到另一个 View

ios - 如何在 iPhone 应用程序中实现订阅计划

ios - MagicalRecord 在 ios8 上崩溃

ios - 未优化与优化二进制文件中的奇怪 ARC 行为

objective-c - UIPicker [__NSCFConstantString _isResizable]错误:?

ios - 为订购应用程序设置核心数据关系