2016-03-01 25 views
1

我想創建一個像這樣的單個UILablelenter image description here如何將NSMutableAttributedString設置爲兩行

我想爲HElvaticaNeu-medium和2015設置年份字體到HelvaticaNeu。我需要這兩個字符串以兩行出現,就像在這張圖片中一樣。

我該怎麼做。我以這種方式設置了兩個獨立的NSMutableAttributedString

UIFont *helMedium = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]; 
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName]; 
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:@"Year :" attributes: mediumDict]; 

UIFont *helNormal = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; 
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName]; 
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"2016" attributes: normalDict]; 

現在應該如何設置這兩個字符串在兩行? 有沒有人有想法?

+0

「\ n」字符就是你要找的。將其添加到helMedium結尾或helNormal字符串的開始處 –

回答

0

你需要第二個字符串追加到第一個字符串

使用此代碼。

UIFont *helMedium = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]; 
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName]; 
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:@"Year :" attributes: mediumDict]; 

UIFont *helNormal = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; 
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName]; 
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict]; 

[mAttrString appendAttributedString:nAttrString]; 

label.attributedText = mAttrString; 
0
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict]; 

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init]; 
[attString appendAttributedString: mAttrString]; 
[attString appendAttributedString: nAttrString]; 

// label.attributedText = attString 
0
UIFont *helMedium = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]; 
    NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName]; 
    NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:@"Year :" attributes: mediumDict]; 

    UIFont *helNormal = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; 
    NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName]; 
    NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict]; 

    [mAttrString appendAttributedString:nAttrString]; 
    _yearLabel.attributedText = mAttrString; 
    [_yearLabel setNumberOfLines:2]; 

如果你想在周圍文本的UILabel更多的空間,指定相同的寬度和高度的限制。

相關問題