0

我有一个UILabel,它有一个字符串。在字符串中的某些文本之间,我必须以粗体/斜体/字母符号添加一些文本(如已注册)。我的应用程序也支持 iOS 4。因此,我使用了另一个标签来显示这些(粗体/斜体/字母符号)文本。

这里的问题是,当我UILabel通过IBOutlet. 它构成下一行,但我希望在我放置另一个标签的行中留出空间。

请给我一些建议,让它按预期工作。任何帮助,将不胜感激。

4

3 回答 3

0

您应该计算每个文本长度并相应地为每个标签设置框架

CGSize size =[titleLabel.text sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(titleLabel.frame.size.width, 200) lineBreakMode:NSLineBreakByWordWrapping];
于 2013-08-07T08:10:58.863 回答
0

你可以用 textview 代替 label

   UITextView *textView=[[UITextView alloc]init];

NSString *String = [NSString stringWithFormat:@"<div><strong style='color:#3468A5;'>This</strong><strong style='color: #4B4B4B;'>is sample string</strong></div> "];
textView.Frame=CGRectMake(10.,10,5,220);
textView.contentInset = UIEdgeInsetsMake(-10,-6,0,0);
[textView setFont:[UIFont fontWithName:@"HelveticaNeueBold" size:14]];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setScrollEnabled:NO];
[textView setEditable:NO];

 @try
{
    SEL setContentToHTMLString = NSSelectorFromString([@[@"set", @"Content", @"To", @"HTML",   @"String", @":"] componentsJoinedByString:@""]);
    [textView performSelector:setContentToHTMLString withObject:String];
}
@catch (NSException *exception)
{
    // html+css could not be applied to text view, so no kerning
}    
于 2013-08-07T08:33:22.680 回答
0

如果要在单个标签中显示不同类型的文本,可以使用NSMutableAttributedString

您也可以NSMutableAttributedString在 6.0 以下的 iOS 中使用,但为此您必须创建一个子类,UILabel并且您必须在drawRect方法中进行自定义。

如果你想在iOS 6.0以下使用属性字符串那么你可以在子类中通过以​​下方式UILabel

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// draw
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)_attributedString);
CGContextSetTextPosition(context, 0.0, 15.0);
CTLineDraw(line, context);

// clean up
CFRelease(line);
}
于 2013-08-07T08:22:04.230 回答