我有一个UILabel
用户编辑并最终在图像上绘制的带有属性的字符串。这是我UILabel
每次用户更改 a 中的文本UITextview
并确认时更新的方法。
- (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+Text 组合到 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;
}
大多数情况下,属性只在生成的图像中显示一两行。它很可能与框架有关。我只是对属性字符串在标签中很好地显示并且在我绘制它时不同的事实感到困惑。
任何有关正确绘制属性字符串的帮助将不胜感激。