UITextView内容插入 [英] UITextView content inset

查看:295
本文介绍了UITextView内容插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在contentInsets中遇到了一些奇怪的东西



我的故事板中有一个UITextView,其中contentInset为50,因为我正在尝试添加一些填充到我的uitextview



但是,uitextview底部会出现一个滚动条,如下面的测试所示:



我认为contentInset会压缩uitextview而不会导致此水平滚动条,所以如何删除对水平滚动条的需要,并且无需使用此滚动条即可显示所有内容 - uitextview中的插图和所有文本。



NB :我不是要求阻止水平滚动或不显示滚动条(因此剪切文本)



非常感谢!



对于atomk(UITextView称为ss)

  NSLog(@%f之前的内容大小, self.ss.contentS ize.width);日志:280 
CGSize size = self.ss.contentSize; size.width = size.width-50;
[self.ss setContentSize:size];
NSLog(@%f之后的内容大小,self.ss.contentSize.width);日志:230

视图与代码之间存在可见差异比添加之前增加了,所以出了问题!
(谢谢)

解决方案

更新:从iOS 7开始,此解决方案已过期。



请参阅下面的

  • 东西你从逆向工程Notes.app 中学习(参见iPadding部分)



  • 希望有所帮助,如果有人能找到更好的解决方案,我很乐意听到它!


    I have encountered something a bit strange with contentInsets

    I have a UITextView in my storyboard with a contentInset of 50 left, as I'm trying to add some padding to my uitextview

    However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:

    I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar, so how can I remove the need for the horizontal scrollbar and make everything--the inset AND all the text in the uitextview--visible without the need for this scrollbar.

    N.B: I'm not asking about preventing the scrolling horizontally or not displaying the scrollbar(thus cutting of the text)

    Thanks a lot!

    For atomk(UITextView is called ss)

    NSLog(@"Content Size Before %f",self.ss.contentSize.width); Logs: 280
    CGSize size=self.ss.contentSize; size.width=size.width-50;
    [self.ss setContentSize:size]; 
    NSLog(@"Content Size After %f",self.ss.contentSize.width); Logs: 230
    

    There is no visible difference between the view with the code added than before it was added, so something's going wrong! (Thanks)

    解决方案

    UPDATE: This solution is out of date as of iOS 7.

    See this answer below. In iOS 7.0, the textContainerInset property on UITextView was introduced:

    textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);
    


    Pre-iOS 7 solution:

    I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...

    I'm afraid this is not how contentInset works with a UITextView. See Apple's documentation for contentInset where it states:

    The distance that the content view is inset from the enclosing scroll view... Use this property to add to the scrolling area around the content.

    The contentInset is added around the content.


    You can change the contentSize in viewDidLayoutSubviews using the code you have included above:

    - (void)viewDidLayoutSubviews
    {
        self.textView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0);
    
        NSLog(@"Content Size Before %f",self.textView.contentSize.width); //Logs: 280
        CGSize size=self.textView.contentSize;
        size.width=size.width-50;
        [self.textView setContentSize:size];
        NSLog(@"Content Size After %f",self.textView.contentSize.width); //Logs: 230
    }
    

    However, this causes the text to be cut off on the right side:


    The best way I have been able to achieve the appearance of horizontal padding in a UITextView is to position it inside a container UIView. In your case, simply create a UIView the same size as your current text view, and add a text view that is 50px narrower inside the container view.

    This workaround can cause problems if you have a background for your text view, but from your screenshot above it doesn't look like that's an issue for you.

    UITextView (frame in red) inside UIView container:

    If your UITextView does have a background, see:

    Hope that helps, and if anyone can find a better solution I'd love to hear about it!

    这篇关于UITextView内容插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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