简体   繁体   English

在iPhone中画线

[英]Drawing strings in iPhone

I am using below method to draw string. 我正在使用以下方法绘制字符串。

-(void) drawRect:(CGRect)rect
{
    NSString *mName = @"Hi what are you doing";
    [mName drawInRect:CGRectMake(0,0,100,12) withFont:[UIFont boldSystemFontOfSize:12] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]
}

Is it possible to draw to "Hi" in different font, "what" in different font in iPhone. 是否有可能在iPhone中以不同的字体绘制为“ Hi”,在iPhone中以不同的字体绘制为“ what”。

Use the method drawAtPoint:withFont: for drawing of each word. 使用方法drawAtPoint:withFont:绘制每个单词。 You must compute the position of each drawn string. 您必须计算每个绘制的字符串的位置。 This can be done with sizeWithFont: -method. 这可以通过sizeWithFont: -method完成。 For example: 例如:

NSString* firstString = @"Hi ";
NSString* secondString = @"what";
// where to draw
CGPoint position = CGPointMake(0, 0); 
CGFloat offset = [firstString sizeWithFont:font1].width + kSpaceBetweenWords;
// draw "Hi" with font1
[firstString drawAtPoint:position withFont:font1]; 
// draw "what" with font2
[secondString drawAtPoint:CGPointMake(position.x+offset, position.y) withFont:font2]; 

No, you need to do two draw requests - one for each font - but that is rather trivial. 不,您需要执行两个绘制请求-每个字体请求一个-但这很简单。

What is not trivial is to know where to place the text if the text is dynamic. 如果文本是动态的,那么要知道将文本放置在何处并不是不重要的。 In that case I would recommend subclassing UIView and the drawRect method and just draw it yourself. 在那种情况下,我建议子类化UIView和drawRect方法,然后自己绘制。

NSAttributedString is available for iOS 3.2 later. NSAttributedString稍后可用于iOS 3.2。 you can try this. 你可以试试看 Or else core text library is there to achieve the same... 否则核心文本库在那里可以实现相同的目标...

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

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