iPadiPhone – NSString drawInRect没有自动换行

iPadiPhone – NSString drawInRect没有自动换行,第1张

概述我正在使用以下内容在UIView中呈现一些文本. - (void) drawRect:(CGRect)rect{ NSString* text = @"asdf asdf asdf asdf asdf asdf asdf"; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillC 我正在使用以下内容在UIVIEw中呈现一些文本.

- (voID) drawRect:(CGRect)rect{    Nsstring* text = @"asdf asdf asdf asdf asdf asdf asdf";    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillcolorWithcolor(context,[[UIcolor clearcolor] CGcolor]);    CGContextFillRect(context,rect);    CGContextSetTextDrawingMode(context,kCGTextFillstrokeClip);    CGContextSetFillcolorWithcolor(context,[[UIcolor whitecolor] CGcolor]);    CGContextSetstrokecolorWithcolor(context,[[UIcolor blackcolor] CGcolor]);    CGContextSetShouldSmoothFonts(context,YES);    UIFont* Font = [UIFont FontWithname:@"ArialRoundedMTBold" size:20.0f];     CGSize textMaxSize = CGSizeMake(rect.size.wIDth - 20.0f,rect.size.height);    CGSize textSize = [text sizeWithFont:Font constrainedToSize:textMaxSize lineBreakMode:UIlineBreakModeWorDWrap];    CGRect textRect = CGRectMake(10.0f,10.0f,textSize.wIDth,textSize.height);    [text drawInRect:textRect withFont:Font];    [text drawInRect:textRect withFont:Font lineBreakMode:UIlineBreakModeWorDWrap];     [text drawInRect:textRect withFont:Font lineBreakMode:UIlineBreakModeWorDWrap alignment:UITextAlignmentCenter];}

[text drawInRect]没有像我期望的那样包装文本. TextSize是正确计算的,但绘制只是渲染一行.

更新:

解决了.

设置CGContextSetTextDrawingMode(context,kCGTextFillstrokeClip);无论选择何种UIlineBreak模式,都会导致文本被剪辑.设置CGContextSetTextDrawingMode(context,kCGTextFillstroke);解决了这个问题.

解决方法 您可能没有textRect中的垂直大小.看看这个类似的问题:

How do I get -[NSString sizeWithFont:forWidth:lineBreakMode:] to work?

总结

以上是内存溢出为你收集整理的iPad / iPhone – NSString drawInRect没有自动换行全部内容,希望文章能够帮你解决iPad / iPhone – NSString drawInRect没有自动换行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存