2013-09-23 38 views
5

Tôi đang làm việc với Bộ công cụ văn bản trong iOS 7 và tôi đang tìm thấy nhiều điểm kỳ quặc xung quanh khu vực loại trừ NSTextContainer.Dòng mới trong iOS 7 UITextView vi phạm gói loại trừ Bộ văn bản

Tôi có hai chế độ xem: một UITextView và một hình kéo đơn giản UIView; khi di chuyển UIView, tôi tạo một đường bezier từ khung của UIView (được điều chỉnh trong không gian tọa độ của UITextView) và tôi cập nhật mảng UITextView '' exclusionPaths - khá đơn giản.

Trong ảnh chụp màn hình đầu tiên, bạn có thể thấy rằng chữ Kit độc đáo kết thúc tốt đẹp văn bản xung quanh một khu vực loại trừ hình chữ nhật:

Exclusion zone with no newlines in text

Tuy nhiên, khi người dùng giới thiệu dòng mới vào UITextView, TextKit dường như nghĩ rằng vùng loại trừ lớn hơn nhiều theo chiều dọc - bởi những gì dường như chính xác cao như khoảng trắng được tạo bởi dòng mới. Con đường bezier là chính xác như nhau, vì vậy điều này có vẻ là một vấn đề Kit văn bản (trừ khi tôi đang làm điều gì đó sai).

Exclusion zone with newlines in text

Code:

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

Bất kỳ suy nghĩ?

Trả lời

5

Tôi đã gặp sự cố tương tự ngay hôm nay.

Lỗi này dường như xuất hiện, nếu bạn đặt editableselectable cùng một lúc. Nếu chỉ có một hoặc không được chọn, nó sẽ hiển thị như mong đợi. Cả hai đều được chọn theo mặc định.

enter image description here
enter image description here

Nếu bạn cần cả hai lựa chọn, chỉ cần đặt chúng trong mã.

_textView.textContainer.exclusionPaths = exclusionPaths; 
_textView.attributedText = attrString; 
_textView.editable = YES; 
_textView.selectable = YES; 
Các vấn đề liên quan