Điều này phù hợp với tôi. Thay đổi textView contentInset và khung inset/position để có được vùng đệm và từ bọc chính xác. Sau đó thay đổi các giới hạn textView để ngăn cuộn ngang. Bạn cũng cần phải đặt lại vùng đệm mỗi khi văn bản được chọn và thay đổi.
- (void)setPadding
{
UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50);
textView.contentInset = padding;
CGRect frame = textView.frame;
// must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets
CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right));
// offset frame back to original x
CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - (padding.left + padding.right)/2);
insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0));
textView.frame = insetFrame;
textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right));
[textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
[self setPadding];
}
-(void)textViewDidChange:(UITextView *)textView
{
[self setPadding];
}
Nguồn
2012-07-12 18:29:09
Yeah, đó là những gì tôi đang làm bây giờ. Hoạt động tốt cho các lề bên lề, nhưng lề trên có vấn đề. Từ quan điểm của người dùng, văn bản được "cắt" ở trên cùng trong khi cuộn vì văn bản chỉ hiển thị trong UITextView. – strawtarget
Và bạn có thể không hiển thị đúng thanh cuộn dọc theo cách này. – an0
Đặt 'textView.clipsToBounds = NO' sẽ" sửa "cắt ở đầu, sắp xếp. Tất nhiên, điều này sẽ không giúp hiển thị thanh cuộn dọc một cách chính xác. – villapossu