UITextView background image

后端 未结 10 1995
说谎
说谎 2020-12-02 08:44

How can I set up a background image to UITextView?

相关标签:
10条回答
  • 2020-12-02 09:30

    I found one simple method how to set background image.

    h file

    @interface FNTextView : UITextView
    
    @end
    

    m file

    ...
    - (void)drawRect:(CGRect)rect
    {
        [self.bgImage drawInRect:rect];
    
        [super drawRect:rect];
    }
    
    - (void)initHandler
    {
        self.bgImage = [[UIImage imageNamed:@"textview_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
    }
    ...
    
    0 讨论(0)
  • 2020-12-02 09:36
    UITextView *textView=[[UITextView alloc]initWithFrame: CGRectMake(20, 20, 40, 40)];
    textView.text = @"this is a test \n this is test \n this is a test";
    UIImageView *img = [[UIImageView alloc]initWithFrame: textView.frame];
    img.image = [UIImage imageNamed: @"image.png"];
    [self.view addSubview:textView];
    [self.view insertSubview:img belowSubview:textView];
    
    0 讨论(0)
  • 2020-12-02 09:37

    Not sure about, but you can try the following, assuming that your background image is handled by a UIImageView:

    [myTextView addSubview: myImageView];

    Note that you may need to change the value of the alpha/opaque properties of your UITextView.

    Kind Regards.

    0 讨论(0)
  • 2020-12-02 09:37

    The answers of @dulcanwilcox and @oxigen both work, but, an addition would be to make the background image resizable, in case the image is being used to show some kind of border.

    UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
    textView.text = @"Some text...";
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
    imgView.image = [[UIImage imageNamed: @"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    
    [textView addSubview:imgView];
    [textView sendSubviewToBack:imgView];
    
    [window addSubview:textView];
    

    Check out the documentation for resizableImageWithCapInsets:(UIEdgeInsets)capInsets.

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