How to set content inset for UITextView in ios

守給你的承諾、 提交于 2019-11-27 15:31:07

问题


I'm having a UITextView and set content inset as

[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];

This code working in iOS 6.1 and below, but nothing happens in iOS 7.0.


回答1:


got answer:

[atextView  setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)];

fixed my problem.

And in Swift...

@IBOutlet var tv:UITextView!
override func awakeFromNib() {
    super.awakeFromNib()
    tv.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

UPDATE: another option to do that.

UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[msgtext setLeftViewMode:UITextFieldViewModeAlways];
[msgtext setLeftView:spacerView];



回答2:


An alternative and maybe a little more convenient way to do it, by subclassing the UITextField and add an @IBInspectable inset property.

import UIKit

class UITextfieldWithInset: UITextField {

    @IBInspectable
    var textfieldInset: CGPoint = CGPointMake(0, 0)

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
    }

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
    }

}


来源:https://stackoverflow.com/questions/19065693/how-to-set-content-inset-for-uitextview-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!