Adding left margin to UITextView

前端 未结 6 709
臣服心动
臣服心动 2021-02-05 17:25

I\'m trying to add a left margin to a UITextView.

I\'ve tried setting the property contentInset, see below:

UITextView *textView = [[UITextView alloc] in         


        
相关标签:
6条回答
  • 2021-02-05 17:46

    The ONLY thing worked for me is by sub-classing the UITextView and overriding the following method:

        - (id)styleString
    {
        return [[super styleString] stringByAppendingString:@"; line-height: 1.6em;margin-right: 30px; margin-left: 45px; margin-top: 20px;"];
    }
    

    Apparently; you can tweak the margin-left, margin-top & whatever you want ;)


    The suggestion about placing the UITextView in a larger UIView didn't work for me as I needed to scrollable lines in the background of UITextView, I used the following open-source project to do that: https://github.com/aahsanali/NoteView

    Also, the following solution didn't help me at all:

    [textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)]
    

    As it wasn't wrapping text correctly.

    I hope this solution helps as I literally spent a week digging into this simple UI tweak!

    0 讨论(0)
  • 2021-02-05 17:47

    UITextView is internally implemented using a web view. These internals uses allot of undocumented tricks, that also changes between OS updates. One of these tricks is to time and again reset the contentInsets property. When and why is not documented, by I have learned that any changes to the layout of the text view can/will trigger this reset.

    My first advice would be to not try to tweak the insets of the text view. It is just to fragile, and will probably break in the future.

    If you really insist anyway then I would subclass UITextView, override -[UITextView layoutSubviews] and force new insets there.

    In either case I would go to http://bugreport.apple.com and report the current behavior as serious bug.

    0 讨论(0)
  • 2021-02-05 17:53

    If you only want to move the text, try

    [textView setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
    

    Where a positive number moves the "text frame" towards the middle, a negative moves it out from the middle.

    For example, [textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)], will move the text 20 pixels to the right!

    0 讨论(0)
  • 2021-02-05 17:59

    You can try this code below with Swift 3.0

    // Align the text to the left edge of textContainer
    textView.textContainer.lineFragmentPadding = 16
    // Align the text with the top of textContainer
    textView.textContainerInset = UIEdgeInsets.zero
    

    It works for me.

    0 讨论(0)
  • 2021-02-05 18:05

    Remember that for iOS 7 there a special property called

    textContainerInset The inset of the text container's layout area within the text view's content area.

    @property(nonatomic, assign) UIEdgeInsets textContainerInset

    This property provides text margins for text laid out in the text view.

    Availability Available in iOS 7.0 and later. Declared In UITextView.h

    in iOS 6 the contentInset is doing the job. I personally encountered this problem. Everything ended up with:

    if (isIOS7()) {
        textView.textContainerInset = UIEdgeInsetsMake(0, 10, 0, 10);
    } else {
        textView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    }
    
    0 讨论(0)
  • 2021-02-05 18:06

    It is much easier if you do it via IB. Find the panel named Scroll View Size & set the Content Insets.

    In code, you can try setting scrollEnabled to NO.

    0 讨论(0)
提交回复
热议问题