如何使用核心文本在ios中的不同UIlabel文本行上设置不同的字体样式

时间:2016-07-15 05:47:59

标签: ios objective-c swift uilabel core-text

我想制作标签格式线这个动态形状的标签文字不同大小如何使用核心文本框架

请在Dynamic arrange label text Diffent font style arrange

上的动态形状生成中显示此类型格式

请在coustom库上建议制作此类型格式

2 个答案:

答案 0 :(得分:0)

我只是向您展示NSAttributedString的方向,您必须弄清楚其他细节,如正确的字体,大小,样式,行间距等。(提示:检查NSMutableParagraphStyle)

    let attr = NSMutableAttributedString(string: "")
    attr.appendAttributedString(NSAttributedString(string: "The\n", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(60), NSForegroundColorAttributeName: UIColor.greenColor()]))
    attr.appendAttributedString(NSAttributedString(string: "HAPPINESS OF\n", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(45), NSForegroundColorAttributeName: UIColor.redColor()]))
    attr.appendAttributedString(NSAttributedString(string: "YOUR LIFE\n", attributes: [NSFontAttributeName: UIFont.italicSystemFontOfSize(40), NSForegroundColorAttributeName: UIColor.purpleColor()]))
    attr.appendAttributedString(NSAttributedString(string: "DEPENDS UPON", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(15), NSForegroundColorAttributeName: UIColor.brownColor()]))
    label.attributedText = attr

enter image description here

答案 1 :(得分:0)

使用objective-C,您需要使用不同的字体名称和大小制作4个NSMutableAttributedString,然后使用“appendAttributedString”附加它们以获得所需的结果。使用这种方法,您只需要为所有字符串创建一个帧。

请参阅以下代码以供参考。

//frame that contains all text
textFrame = [[UILabel alloc] init];
[textFrame setTextAlignment:NSTextAlignmentCenter];
[textFrame setNumberOfLines:0];
[self.view addSubview:textFrame];

NSString *text1 = @"THE";
NSString *text2 = @"\nHAPPINESS OF";
NSString *text3 = @"\nYOUR LIFE";
NSString *text4 = @"\nDEPENDS UPON";

//change font name and size according to your need.
UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 =
[[NSMutableAttributedString alloc] initWithString:text1 attributes:@{
NSFontAttributeName : text1Font}];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment:NSTextAlignmentCenter];
[paragraphStyle1 setLineSpacing:4];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];

UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:4];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];

UIFont *text3Font = [UIFont fontWithName:@"HelveticaNeue" size:14];
NSMutableAttributedString *attributedString3 =
[[NSMutableAttributedString alloc] initWithString:text3 attributes:@{NSFontAttributeName : text3Font }];
NSMutableParagraphStyle *paragraphStyle3 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle3 setAlignment:NSTextAlignmentCenter];
[attributedString3 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle3 range:NSMakeRange(0, [attributedString3 length])];

UIFont *text4Font = [UIFont fontWithName:@"HelveticaNeue" size:14];
NSMutableAttributedString *attributedString4 =
[[NSMutableAttributedString alloc] initWithString:text4 attributes:@{NSFontAttributeName : text4Font }];
NSMutableParagraphStyle *paragraphStyle4 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle4 setAlignment:NSTextAlignmentCenter];
[attributedString4 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle4 range:NSMakeRange(0, [attributedString4 length])];

[attributedString1 appendAttributedString:attributedString2];
[attributedString1 appendAttributedString:attributedString3];
[attributedString1 appendAttributedString:attributedString4];

[textFrame setAttributedText:attributedString1];
[textFrame sizeToFit];
//change frame size as per your need.
[textFrame setFrame:CGRectMake(10, 0, 136, 97)];