2013-08-07 61 views
0

我有一個UILabel,它有一個字符串。在字符串中的某些文本之間,我必須添加一些文字加粗/斜體/字母符號(如註冊)。我的應用程序也支持iOS 4.所以,我拿了另一個標籤來顯示這些(粗體/斜體/字母符號)文本。如何通過IBOutlet在UILabel中的字符串之間添加換行符?

這裏的問題是,當我通過IBOutletUILabel文本中給出空間時。它使下一行,但我想要放在另一個標籤的行中的空間。

請建議我讓它按預期工作。任何幫助,將不勝感激。

+0

顯示一些屏幕截圖。遵循你的問題有點令人困惑。 –

+0

對於iOS4,使用['TTTAttributedLabel'](https://github.com/mattt/TTTAttributedLabel)顯示屬性文本。下面的AFAIK,你需要通過代碼添加這個,不能使用接口生成器。 – Amar

+0

@Sudhir我編輯了你的問題。如果我的編輯錯誤,請毫不猶豫地恢復更改。 –

回答

0

您應該計算每個文本長度和每個標籤的相應設置幀

CGSize size =[titleLabel.text sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(titleLabel.frame.size.width, 200) lineBreakMode:NSLineBreakByWordWrapping]; 
0

如果你想顯示不同類型的單個標籤的文字,你可以用NSMutableAttributedString

您可以在iOS中使用NSMutableAttributedString也低於6.0,但爲此您必須創建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); 
} 
+0

它不適用於ios4 –

+0

你提出的建議只適用於iOS6和以上的版本。 OP已經要求iOS4。 – Amar

+0

@Amar誰說NSMutableAttributedString不適用於iOS 4?由Discover發出的答案也是有效的。但我不知道,如果這個答案對OP有幫助。如果你給他-1,然後刪除它。 http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html –

0

子類中的以下方法,你可以TextView的,而不是標籤

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 
}  
+0

如果你能解釋你的答案,那麼OP會很高興。 –

+0

我用HTML字符串與文本視圖。 (這是什麼解釋) –

+0

@ChamanSharma,謝謝你的建議。但我有一個字符串不是HTML。如果我有一個HTML字符串,Web視圖可以顯示數據。 – Sudhir

相關問題