Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使用三维阴影边框设置UITextView的样式?_Iphone_3d_Coding Style_Uitextview_Border - Fatal编程技术网

Iphone 如何使用三维阴影边框设置UITextView的样式?

Iphone 如何使用三维阴影边框设置UITextView的样式?,iphone,3d,coding-style,uitextview,border,Iphone,3d,Coding Style,Uitextview,Border,在另一篇文章中,分享了一种方法,通过这种方法,可以使用QuartzCore框架()将圆角应用于文本视图。在我看来,一个3D边框,就像在UITextField上发现的那样,可以通过层而不是使用背景图像来创建 有人知道这是否可以或如何做到吗?我真的很想找到一种方法来添加3D边框,而不必启动图形编辑器并创建3D阴影背景。谢谢 视图中的控制器: newCommentBody.layer.cornerRadius = 7; newCommentBody.clipsToBounds =

在另一篇文章中,分享了一种方法,通过这种方法,可以使用QuartzCore框架()将圆角应用于文本视图。在我看来,一个3D边框,就像在UITextField上发现的那样,可以通过层而不是使用背景图像来创建

有人知道这是否可以或如何做到吗?我真的很想找到一种方法来添加3D边框,而不必启动图形编辑器并创建3D阴影背景。谢谢

视图中的控制器:

    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
#导入“TextView.h”
#进口
#进口
@实现文本视图
-(void)drawRect:(CGRect)rect{
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef currentContext=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext,1.0);//或任意宽度
CGContextSetRGBStrokeColor(currentContext,0.6,0.6,6,1.0);
CGRect myRect=CGContextGetClipboondingBox(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设置图像:背景图像];
[自添加子视图:myImageView];
[背景图片发布];
UIGraphicsSendImageContext();
}
@结束

它不是3D的,但它更简单、更安全的代码

很接近!但是,TextView中的阴影边框不会“粘贴”边缘。如果输入的文本大于视图大小(即滚动),阴影边框将与文本视图中的文本一起滚动。需要修改自定义TextView以根据文本内容调整边框大小。为了使阴影不滚动,您可以使用TextView的框架创建一个“容器”视图,然后绘制阴影,然后在其中添加TextView。这是一个很好的解决方案。只是复制和粘贴,它完全没有问题。我也尝试实现它-但是我得到了黑色背景,而不是3D阴影。知道为什么吗?
m_txtViewSource.layer.borderWidth = 1;

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