2012-11-27 3 views
5

UIView 클래스의 UIBezierpath을 사용하여 연설 거품을 그립니다. 뷰 클래스에는 UITextView가 포함되어 있습니다. 연설 거품을 그릴 때 다음 코드를 사용하고 있습니다.연설 거품을 UITextview로 그리는 방법 ContentSize

//BezierPathView.h

@interface BezierpathView : UIView<UITextViewDelegate>{ 

CGFloat animatedDistance; 

UITextView *LXVolabel; 
UIView *view; 
UIBezierPath* speechBubbleTopPath; 
UIBezierPath* rectanglePath; 
UIBezierPath* speechBubbleBottomPath; 
UIColor* darkGray; 
UIColor* shadow; 
CGSize shadowOffset; 
CGFloat shadowBlurRadius; 
NSString* textContent; 
} 
@property(nonatomic,strong)UIView *view; 
@property(nonatomic,strong)UITextView *LXVolabel; 

//BezierPath.m

@implementation BezierpathView 
@synthesize LXVolabel,view; 
- (id)initWithFrame:(CGRect)frame{ 
self = [super initWithFrame:frame]; 
if (self) { 
    self.backgroundColor = [UIColor clearColor]; 
    self.LXVolabel = [[UITextView alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height-10)]; 
    self.LXVolabel.backgroundColor = [UIColor clearColor]; 
    self.LXVolabel.delegate = self; 
    self.LXVolabel.font = [UIFont systemFontOfSize:20]; 

    [self addSubview:self.LXVolabel]; 

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, applicationFrame.size.height)]; 


    // Color Declarations 
    darkGray = [UIColor grayColor]; 

    // Shadow Declarations 
    shadow= [UIColor blackColor]; 
    shadowOffset= CGSizeMake(0, 1); 
    shadowBlurRadius= 1; 

    // Abstracted Graphic Attributes 
    textContent= LXVolabel.text; 

} 
return self; 
} 
- (void)drawRect:(CGRect)rect 
{ 
[super drawRect:rect]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Drawing code 

//// General Declarations 

// speechBubbleTop Drawing 
speechBubbleTopPath = [UIBezierPath bezierPath]; 
[speechBubbleTopPath moveToPoint: CGPointMake(294, 7)]; 
[speechBubbleTopPath addCurveToPoint: CGPointMake(288, -0) controlPoint1: CGPointMake(294, 3.13) controlPoint2: CGPointMake(291.31, -0)]; 
[speechBubbleTopPath addLineToPoint: CGPointMake(8, -0)]; 
[speechBubbleTopPath addCurveToPoint: CGPointMake(2, 7) controlPoint1: CGPointMake(4.69, -0) controlPoint2: CGPointMake(2, 3.13)]; 
[speechBubbleTopPath addLineToPoint: CGPointMake(294, 7)]; 
[speechBubbleTopPath closePath]; 
[darkGray setFill]; 
[speechBubbleTopPath fill]; 



    // Rectangle Drawing 
    rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(2, 6, 292,self.frame.size.height-15)]; 
CGContextSaveGState(context); 
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor); 
[darkGray setFill]; 
[rectanglePath fill]; 
CGContextRestoreGState(context); 



// Text Drawing 
CGRect textRect = CGRectMake(7, 6, 292, self.frame.size.height-15); 
CGContextSaveGState(context); 
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor); 
[[UIColor whiteColor] setFill]; 
[textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica-Light" size: 14] lineBreakMode: NSLineBreakByWordWrapping alignment: NSTextAlignmentLeft]; 
//CGContextRestoreGState(context); 

float addedHeight = 100 -38; 

[self drawPath:addedHeight contextValue:context]; 
//speechBubbleBottom Drawing 


speechBubbleBottomPath = [UIBezierPath bezierPath]; 
[speechBubbleBottomPath moveToPoint: CGPointMake(2, 24+addedHeight)]; 
[speechBubbleBottomPath addCurveToPoint: CGPointMake(8, 30+addedHeight) controlPoint1: CGPointMake(2, 27.31+addedHeight) controlPoint2: CGPointMake(4.69, 30+addedHeight)]; 
[speechBubbleBottomPath addLineToPoint: CGPointMake(13, 30+addedHeight)]; 
[speechBubbleBottomPath addLineToPoint: CGPointMake(8, 42+addedHeight)]; 
[speechBubbleBottomPath addLineToPoint: CGPointMake(25, 30+addedHeight)]; 
[speechBubbleBottomPath addLineToPoint: CGPointMake(288, 30+addedHeight)]; 
[speechBubbleBottomPath addCurveToPoint: CGPointMake(294, 24+addedHeight) controlPoint1: CGPointMake(291.31, 30+addedHeight) controlPoint2: CGPointMake(294, 27.31+addedHeight)]; 
[speechBubbleBottomPath addLineToPoint: CGPointMake(2, 24+addedHeight)]; 
[speechBubbleBottomPath closePath]; 
CGContextSaveGState(context); 
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor); 
[darkGray setFill]; 
[speechBubbleBottomPath fill]; 
CGContextRestoreGState(context); 

} 
-(void)textViewDidChange:(UITextView *)textView{ 
CGRect rect = textView.frame; 
rect.size.height = textView.contentSize.height;// Adding.size Since height is not a member of CGRect 
    self.frame = CGRectMake(10, 20, textView.frame.size.width, textView.contentSize.height+20); 
    textView.frame = rect; 
} 

나의 생각은 내가 텍스트 뷰의 텍스트를 입력 할 때 내가 연설 거품의 크기를 증가 할 것입니다 텍스트보기의 텍스트를 기반으로합니다. 하지만 내 연설 거품 크기가 올바르게 증가하지 않습니다. 이 문제를 어떻게 해결합니까?

+3

멋진 질문 형제 – Ben10

답변

1

텍스트를 바꿀 때 다시 그리기를 실행 하시겠습니까? textViewDidChange 방법에

추가

... 마지막 줄로
[self setNeedsDisplay]; 

...

.

이렇게하면 drawRect 메서드가 다시 트리거됩니다.

+0

textViewdidChange 내에서 CGContexRef 메서드를 호출 할 수 없습니다. –

+0

필요하지 않습니다. 내가 말한 코드 한 줄을 추가하면된다. [sepf setNeedsDisplay]; 아무것도 – Fogmeister

+0

고마워요. 완벽하게 작동했습니다. –

관련 문제