1

我有文本的 UITextView,在用户按下 DONE 后,我将文本转换为 UIImageView 并显示它。它适用于一行文本非常好,

截图 1

. 但是如果用户键入两行或更多:结果仍然是一行???截图 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; 
}
4

3 回答 3

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];
于 2012-12-08T05:02:16.327 回答
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; 
}
于 2012-12-08T03:36:17.530 回答
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;

}

于 2012-12-08T10:40:28.563 回答