2015-08-03 83 views
0

如何使用UIImageView在左上角編寫用於UITextView的Xcode Objective-C代碼,並在UIImageView旁邊字符串啓動。UIImageView需要左上角UITextView

所以UITextView文本不會填寫矩形區域。 我想過如何製作包含UIImageView的UITextView的子類。但是,然後,我堅持不應穿過UIImageView區域的文本定位。

在此先感謝。

+0

謝謝大家徵求意見。 – nelm

回答

3
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(100, 150, 100, 60)]; 
    [textView setTextContainerInset:UIEdgeInsetsMake(0, 30, 0, 0)];//30** 
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 60)];//30** 
    [imageView setImage:[UIImage imageNamed:@"imageName"]]; 
    [textView addSubview:imageView]; 
    [self.view addSubview:textView]; 

30 ** = imageWidth = edgeInset爲萊夫特賽德

2

試試這個

myTextField.leftViewMode = UITextFieldViewModeAlways; 
    myTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]]; 

希望它能幫助。

+0

問題是UITextView不是UITextField – stan

2

試試吧豬頭...

func addButtonInTextField(image:UIImage, forTextField text:UITextField) ->UIButton{ 

    let kImageEdge:CGFloat = 2.0 
    let ysFix_button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
    ysFix_button.frame = CGRectMake(text.frame.origin.x, text.frame.origin.y, text.frame.size.width/7, text.frame.size.height) 
    ysFix_button.imageEdgeInsets = UIEdgeInsetsMake(kImageEdge,kImageEdge,kImageEdge,kImageEdge) 
    ysFix_button.setImage(image, forState: UIControlState.Normal) 

    text.rightView = ysFix_button 
    text.rightViewMode = UITextFieldViewMode.Always 
    return ysFix_button 
}