iphone - 如何使用 3D 阴影边框设计 UITextView 的样式?

标签 iphone 3d coding-style uitextview border

在另一篇文章中,luvieere分享了一种使用 QuartzCore 框架将圆角应用于 TextView 的方法(see post)。在我看来,3D 边框(如 UITextField 上的边框)可以通过图层创建,而不是使用背景图像。

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

最佳答案

在 View Controller 中:

    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

关于iphone - 如何使用 3D 阴影边框设计 UITextView 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991836/

相关文章:

c# - 具有不同返回类型的方法重载

c++ - 你最不喜欢的 C++ 编码指南

iphone - 如何为一个属性添加多个实体(iOS,核心数据)?

c++ - opengl - 如何在 3D 不规则物体上放置纹理

java - 围绕球体移动相机

3d - 消失点如何帮助恢复正确的相机视角?

iphone - 如何使用 scanf 接受 NSString 中的值?

ios - 使用 UIBezierPath 和阴影查看?

ios - 我们怎样才能只从谷歌驱动器 ios 获取文件夹和 pdf 文件

android - 如何将主题/样式应用于 Action android