如何将语音气泡绘制为UITextView的ContentSize

5
我将使用UIBezierPathUIView类中绘制对话气泡。在该视图类中包含UITextView。我将使用以下代码来绘制对话气泡。

//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;
 }

我的想法是,当我在文本视图中输入文本后,我希望根据文本的长度增加气泡的大小。但是我的气泡大小没有正确增加。我该如何解决这个问题?

1个回答

1
你是否在文本更改时触发了重绘?
在textViewDidChange方法中添加...
[self setNeedsDisplay];

...作为最后一行。

这将再次触发drawRect方法。


我无法在textViewdidChange方法中调用CGContexRef方法。 - user1208852
你不需要做其他的事情,只需添加我说的一行代码 [sepf setNeedsDisplay]; - Fogmeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接