2010-01-02 62 views
3

在另一篇文章中,luvieere分享了使用QuartzCore框架(see post)將圓角應用於文本視圖的方法。在我看來,像UITextField中發現的3D邊框可以通過圖層創建,而不是使用背景圖像。如何使用3D陰影邊框設計UITextView?

有誰知道是否或如何做到這一點?我真的很想找到一種方法來添加3D邊框,而不必啓動圖形編輯器並創建3D陰影背景。謝謝!

回答

4

在視圖控制器:

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

結交新類的TextView繼承的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 
+0

這是接近!但是,TextView中的陰影邊框不會「粘住」邊緣。如果輸入的文本大於視圖大小(即滾動),則陰影邊框將與TextView中的文本一起滾動。需要修改自定義TextView來調整每個文本內容的邊框大小。 – DSpeckleback 2010-04-15 17:39:40

+0

您可以通過縮進8個空格來格式化代碼。 – kennytm 2010-07-03 15:59:39

+0

對於不滾動的陰影,可以使用textview的框架創建一個「容器」視圖,然後繪製陰影,然後在其中添加文本視圖。 – 2010-09-16 10:50:25

1
m_txtViewSource.layer.borderWidth = 1; 

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

它不是3D,但更爲簡單和安全的代碼