2014-01-25 25 views
0

我有一個UILabel用戶編輯並最終在圖像上繪製的屬性字符串。每當用戶更改UITextview中的文字並確認時,我都會更新UILabelAttributedString有時不能成功繪製使用drawInRect

- (void)displayPicture { 
    [self.view endEditing:YES]; 
    self.attributedStr = [[NSAttributedString alloc] initWithString:self.textInputField.text attributes:self.attributes]; 
    self.displayField.attributedText = [[NSAttributedString alloc] initWithString:self.textInputField.text attributes:self.attributes]; 
    self.displayField.lineBreakMode = NSLineBreakByCharWrapping; 
    [self.displayField sizeToFit]; 
    [self.displayField setNeedsDisplay]; 
    self.textInputField.hidden = YES; 
    self.tapRecog.enabled = YES; 
    self.displayField.hidden = NO; 
} 

現在,UILabel顯示屏幕上所需的方式。

當用戶想要在PIC +文本組合成的UIImage並張貼,下面的代碼被實現:

- (UIImage *)generatePicture { 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(Picture_Standard_Width, Picutre_Standard_Height), YES, 0.0f); 
[self.bgImageView.image drawInRect:CGRectMake(0, 0, Picture_Standard_Width, Picutre_Standard_Height)]; 
CGRect originalFrame = self.displayField.frame; 
CGRect adjustedFrame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y - self.bgImageView.frame.origin.y, originalFrame.size.width, originalFrame.size.height); 
[self.displayField.attributedText drawInRect:adjustedFrame]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
self.imageToUpload = newImage; 
UIGraphicsEndImageContext(); 
return self.imageToUpload; 
} 

大部分歸因時代的僅示出了所產生的圖像中的一行或兩行。這很可能與框架有關。我簡單地被屬性字符串很好地顯示在標籤中,而當我繪製它時會有所不同。

正確繪製屬性字符串的任何幫助將不勝感激。

回答

0

當你開始一張圖片時,你開始繪製一個新的上下文。

上下文是圖像,0,0是圖像角落。

所以你必須調整你的originalFrame,考慮到你現在不再繪製到視圖的上下文中,而是進入圖像的上下文中