3

In another post, luvieere shared a means by which rounded corners can be applied to a text view using the QuartzCore framework (see post). It would seem to me that a 3D border, like that found on a UITextField, could be created via layers instead of using a background image.

Does anyone know if or how this can be done? I'd really like to find a method to add a 3D border WITHOUT having to fire up a graphics editor and create a 3D shadowed background. Thanks!

Community
  • 1
  • 1
DSpeckleback
  • 31
  • 1
  • 6

2 Answers2

4

In View Controller:

    newCommentBody.layer.cornerRadius = 7;  
    newCommentBody.clipsToBounds = YES;  

Make new class TextView inherits UITextView

#import "TextView.h"
#import <CoreGraphics/CoreGraphics.h>
#import <CoreGraphics/CGColor.h>

@implementation TextView

-(void) drawRect:(CGRect)rect {
    UIGraphicsBeginImageContext(self.frame.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 1.0); //or whatever width you want
    CGContextSetRGBStrokeColor(currentContext, 0.6, 0.6, .6, 1.0);

    CGRect myRect = CGContextGetClipBoundingBox(currentContext);  
    //printf("rect = %f,%f,%f,%f\n", myRect.origin.x, myRect.origin.y, myRect.size.width, myRect.size.height);

    float myShadowColorValues[] = {0,0,0,1};
    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorRef = CGColorCreate(myColorSpace, myShadowColorValues);
    CGContextSetShadowWithColor(currentContext, CGSizeMake(0, -1), 3, colorRef);
    // CGContextSetShadow(currentContext, CGSizeMake(0, -1), 3);

    CGContextStrokeRect(currentContext, myRect);
    UIImage *backgroundImage = (UIImage *)UIGraphicsGetImageFromCurrentImageContext();
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [myImageView setImage:backgroundImage];
    [self addSubview:myImageView];
    [backgroundImage release];  

    UIGraphicsEndImageContext();
}

@end
newonder
  • 41
  • 3
  • This is close! However, the shadowed borders within the TextView don't "stick" the the edges. If text is entered that is bigger than the view size (i.e., scrolling), the shadowed border will scroll with the text within the TextView. Modifications to the custom TextView are needed to adjust the border size per the text content. – DSpeckleback Apr 15 '10 at 17:39
  • For the shadow not to scroll, you can create a "container" view with the textview's frame, and draw the shadow then add the textview in it. – Eino Gourdin Sep 16 '10 at 10:50
  • was a great solution for what i am looking for. just copied and pasted and it fully worked without a problem. – iremk Jun 19 '12 at 12:06
  • I tried to implement it as well - however I get a black background and not a 3D shadow. Any idea why? – Dejell Dec 05 '12 at 05:49
1
m_txtViewSource.layer.borderWidth = 1;

m_txtViewSource.layer.borderColor = [[UIColor grayColor] CGColor];

it's not 3D but it simpler and safe code

Ankit
  • 6,554
  • 6
  • 49
  • 71
yoni
  • 193
  • 2
  • 7