我有UITextView文本,用户按DONE后,我将文本转换为UIImageView并显示它。它适用于一行非常好的文本,
。但是如果用户键入两行或更多行:结果仍然是一行??? Screenshot 2
我想在UIimageView中显示两行或更多行
任何人都可以帮助我!非常感谢你!
这是我的代码:
-(UIImage *)convertTextToImage : (ObjectText *) objT;
{
UIGraphicsBeginImageContext(CGSizeMake(([objT.content sizeWithFont:objT.font].width+10), ([objT.content sizeWithFont:objT.font].height+10)));
[[objT getcolor] set];
[objT.content drawAtPoint:CGPointMake(5, 5) withFont:objT.font];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
答案 0 :(得分:0)
您的问题是您依靠sizeWithFont
方法来完全控制标签的大小。在不限制宽度的情况下,标签将始终扩展到足够宽的框架,以便文本显示在一行上。
尝试此操作(代码会将宽度限制为您指定的宽度):
-(UIImage *)convertTextToImage : (ObjectText *) objT;
{
//THIS IS THE MAX WIDTH YOU WANT, and anything wider would wrap to two lines
CGFloat desiredWidth = 100.0;
CGSize sizeToConstrainTo = CGSizeMake(desiredWidth, 5000);
CGSize newSize = [objT.content sizeWithFont:objT.font
constrainedToSize:sizeToConstrainTo
lineBreakMode:UILineBreakModeWordWrap];
UIGraphicsBeginImageContext(newSize);
[[objT getcolor] set];
[objT.content drawAtPoint:CGPointMake(5, 5) withFont:objT.font];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
答案 1 :(得分:0)
抽象属性
NSString* textContent = @"Hello, World!";
NSString* text2Content = @"Hello, World!";
NSString* text3Content = @"Hello, World!";
NSString* text4Content = @"Hello, World!";
文字绘图 - 1
CGRect textRect = CGRectMake(65, 43, 56, 55);
[[UIColor blackColor] setFill];
[textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
文字绘图 - 2
CGRect text2Rect = CGRectMake(77, 79, 37, 31);
[[UIColor blackColor] setFill];
[text2Content drawInRect: text2Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
文字绘图 - 3
CGRect text3Rect = CGRectMake(118, 74, 49, 43);
[[UIColor blackColor] setFill];
[text3Content drawInRect: text3Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
文字绘图 - 4
CGRect text4Rect = CGRectMake(71, 17, 41, 28);
[[UIColor blackColor] setFill];
[text4Content drawInRect: text4Rect withFont: [UIFont fontWithName: @"Helvetica" size: 12] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];
答案 2 :(得分:0)
这是我的答案,我认为这是好方法。试试这段代码:
- (UIImage *)convertTextToImage:(ObjectText *)objT; { UIGraphicsBeginImageContext(CGSizeMake([objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900,900)]。width + 10,[objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900,900)]。height + 10) );
[[objT getcolor] set];
[objT.content drawInRect:CGRectMake(5, 5, [objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900, 900)].width, [objT.content sizeWithFont:objT.font constrainedToSize:CGSizeMake(900, 900)].height) withFont:objT.font];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}