2013-09-28 25 views
29

设置内容为插图作为UITextView的如何我有一个UITextView并将内容插入IOS

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

此代码的iOS 6.1及以下的工作,但没有任何反应iOS中7.0。

+1

有没有办法做到这一点**在故事板**?! – Fattie

+0

你试过更新的答案了吗? –

+0

是的,请查看下面的解决方案Joe Blow –

回答

115

得到了回答:

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

固定我的问题。

而且在斯威夫特...

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

UPDATE:另一种选择这样做。

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

嘿@ murugha23,我需要你帮助你如何解决IOS7中的textview setContentInset问题 – Anand

+4

替换'setTextContainerInset'而不是'setContentInset'。如果你想提供以前版本的ios,那么检查一个条件章节,当前设备系统版本 –

+1

是的亲爱的我做到了,但它不适合我 – Anand

1

另一种也许有点更便捷的方式来做到这一点,通过继承的UITextField并添加@IBInspectable插图性质。

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) 
    } 

} 
+0

这是'UITextField'的一个很好的答案,但问题是'UITextView' – dulgan

相关问题