繁体   English   中英

如何绘制多行文本到uiimageview iPhone?

[英]How to draw multiple line text to uiimageview iphone?

我有用于文本的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; 
}

您的问题是您依靠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; 
}

抽象属性

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];

这是我的答案,我认为这是个好方法。 试试这个代码:

-(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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM