开发者

iPhone: UILabel of variable length, multi-line strings with multiple fonts

I would like to do something like this on the iPhone with a UILabel:

John Doe, Jane Doe, John Smith,

Jane Smith like this photo.

I could draw my own text and have it implement multiple fonts on the same line, like in the question here, but once it spans multiple lines, that sort of solution doesn't really seem to work because the sizeWithFont:forWidth:lineBreakMode: method returns a CGRect, and I'd imagine it either to make something like this:

John Doe, Jane Doe, 开发者_运维技巧John Smith, Jane Smith like this photo.

Or like this:

John Doe, Jane Doe, John Smith,

Jane Smith

like this photo.

But I'd like to continue the 2nd font right where the 1st font left off and on the same line. Is there any way to make this happen on the iPhone?


I have used Core Text to handle texts like these to display different fonts at certain places. Core Text gives you a lot of flexibility in terms of changing font, size and setting paragraph attributes. More information about Core text can be found here http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html

To give you an example of how it is done:

-(void) drawLayer:(CALayer *)layerToDraw inContext:(CGContextRef)context {
NSMutableAttributedString* someString = [[NSMutableAttributedString alloc] initWithString:@"New String"];
CTFontRef font = CTFontCreateWithName(@"Arial", 25, NULL);
[someString addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 3)];
CFRelease(font);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, labelLayer.bounds);

CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, labelLayer.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(someString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

CTFrameDraw(frame, context);


CFRelease(framesetter);
CFRelease(frame);
CFRelease(path);
}

But if such usage is very less in your project, you can just use UIWebView to show such text (remember that this consumes more resources)


There is a way to set different / multiple fonts & other properties on Label using NSMutableAttributedString. Foll is my code:

 UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
 NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];    
 NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];

 UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
 NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
 NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];    
 [VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];

 [AattrString appendAttributedString:VattrString];


 lblText.attributedText = AattrString;

Note that lblText is the UILabel, outlet as file owner. One can keep on appending as many NSMutableAttributedString he wants..

Also Note that I've added verdana & arial font in my project & added a plist for the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜