【问题标题】:iPhone: UILabel of variable length, multi-line strings with multiple fontsiPhone:可变长度的UILabel,具有多种字体的多行字符串
【发布时间】:2011-08-26 06:40:31
【问题描述】:

我想用 UILabel 在 iPhone 上做这样的事情:

约翰·多伊,简·多伊,约翰·史密斯,
简·史密斯
赞了这张照片。

我可以绘制我自己的文本并让它在同一行上实现多种字体,就像在question here 中一样,但是一旦它跨越多行,这种解决方案似乎就不起作用了,因为sizeWithFont:forWidth:lineBreakMode:方法返回一个CGRect,我想它要么做这样的事情:

John Doe、Jane Doe、John Smith、Jane Smith喜欢这张照片。

或者像这样:

约翰·多伊,简·多伊,约翰·史密斯,
简·史密斯

喜欢这张照片。

但我想在第一个字体离开的地方和同一行继续第二个字体。有什么方法可以在 iPhone 上实现这一点?

【问题讨论】:

  • 如果使用three20库是一个选项,那么你可以使用three20 TTStyledText的textFromXHTML函数。示例用法可以在 TTCalog 项目中找到。这是TTStyledText 的文档
  • UITextView 对象可能对您更有用(将其设置为不可编辑,您将从它获得非常接近UILabel,具有更大的灵活性)。

标签: iphone objective-c ios fonts uilabel


【解决方案1】:

有一种方法可以使用 NSMutableAttributedString 在标签上设置不同/多种字体和其他属性。 Foll 是我的代码:

 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;

请注意,lblText 是 UILabel,作为文件所有者的出口。 可以继续添加他想要的任意数量的 NSMutableAttributedString..

另外请注意,我在我的项目中添加了 verdana 和 arial 字体,并为此添加了 plist。

【讨论】:

    【解决方案2】:

    我使用 Core Text 来处理这样的文本,以便在某些地方显示不同的字体。 Core Text 在更改字体、大小和设置段落属性方面为您提供了很大的灵活性。有关核心文本的更多信息可以在这里找到http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html

    举个例子说明它是如何完成的:

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

    但是如果你的项目中这样的使用非常少,你可以只使用 UIWebView 来显示这样的文本(记住这会消耗更多的资源)

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多